DevYK 3 лет назад
Родитель
Сommit
3a9d54bbb8
5 измененных файлов с 122 добавлено и 7 удалено
  1. 2 0
      config/dev_config.js
  2. 2 0
      config/prd_config.js
  3. 2 0
      config/test_config.js
  4. 51 4
      model/utils.js
  5. 65 3
      routes/sdk.js

+ 2 - 0
config/dev_config.js

@@ -1,6 +1,7 @@
 const cryppt_config = {
     KEY: 'NENET_CRYPT_KEY',
     KEY_IV: 'NENET_CRYPT_IV',
+    WITHDRAW_KEY:'1234567890123456',
 }
 
 
@@ -92,5 +93,6 @@ module.exports = {
     timer_config,
     logger_config,
     http_log_report_config,
+    cryppt_config,
 }
 

+ 2 - 0
config/prd_config.js

@@ -14,6 +14,7 @@ const account_config = {
 const cryppt_config = {
     KEY: 'NENET_CRYPT_KEY',
     KEY_IV: 'NENET_CRYPT_IV',
+    WITHDRAW_KEY:'',
 }
 
 const reids_token_config = {
@@ -77,5 +78,6 @@ module.exports = {
     timer_config,
     logger_config,
     http_log_report_config,
+    cryppt_config,
 }
 

+ 2 - 0
config/test_config.js

@@ -14,6 +14,7 @@ const account_config = {
 const cryppt_config = {
     KEY: 'NENET_CRYPT_KEY',
     KEY_IV: 'NENET_CRYPT_IV',
+    WITHDRAW_KEY:'1234567890123456',
 }
 
 const reids_token_config = {
@@ -77,5 +78,6 @@ module.exports = {
     timer_config,
     logger_config,
     http_log_report_config,
+    cryppt_config,
 }
 

+ 51 - 4
model/utils.js

@@ -2,6 +2,9 @@
 var CryptoJS = require("crypto-js");
 require('dotenv').config()
 const logger = require('./logger')
+
+var { cryppt_config } = require('../config/config.js')
+
 function toJson(code_, obj_, errMsg_) {
     var code = code_
     var data = obj_
@@ -10,6 +13,10 @@ function toJson(code_, obj_, errMsg_) {
     return JSON.stringify(jopt)
 }
 
+function decryptJson(message, key) {
+
+}
+
 /**
  * 
  * @param {*} crypt_key 密文
@@ -24,8 +31,7 @@ function decryptPrivityKey(message) {
     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)
-    {
+    if (!key || !iv) {
         logger.error('decryptPrivityKey key or iv is empty?');
         return null;
     }
@@ -42,6 +48,45 @@ function decryptPrivityKey(message) {
 }
 
 
+// 密钥
+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',
@@ -62,9 +107,9 @@ function getChainId(key) {
     return CHAIN_ID[key];
 }
 
-function sleep (time) {
+function sleep(time) {
     return new Promise((resolve) => setTimeout(resolve, time));
-  }
+}
 
 module.exports = {
     toJson,
@@ -74,4 +119,6 @@ module.exports = {
     CHAIN_NAME,
     CHAIN_ID,
     sleep,
+    encrypt,
+    decrypt
 }

+ 65 - 3
routes/sdk.js

@@ -53,7 +53,7 @@ async function getTransfers(ctx) {
                         redis.redis_set(reids_token_config.LAST_TOTAL_BNB_FREE, tr.totalGasFree.toString());
                         redis.redis_set(reids_token_config.LAST_TOTAL_TOKEN_FREE, (parseInt(tr.gas_price) * parseInt(account_config.TOKEN_GAS_LIMIT)).toString());
                     }
-                    
+
                     //提交归集任务
                     if (temp_obj.address) {
                         logger.log('pushCollectConisObj>>>', temp_obj.address)
@@ -92,6 +92,67 @@ async function getAllTokenWithdrawInfoLists(ctx) {
     ctx.body = await moralis.getAllTokenWithdrawInfoLists(ctx);
 }
 
+/**
+ * 
+ * @param {鉴权版本} ctx 
+ */
+async function withdrawV2(ctx) {
+    if (ctx.request == null || ctx.request.body == null) {
+        ctx.body = utils.toJson(-1, null, "request error. ");
+        return
+    }
+    const obj = ctx.request.body;
+    var log_obj = { ...obj }
+
+    // const encryptText = utils.encrypt(log_obj);
+    const encryptText = log_obj.content;
+    console.log("加密", encryptText);
+
+    let decryptObj = utils.decrypt(encryptText);
+    try {
+        console.log("解密 before", decryptObj);
+        decryptObj = JSON.parse(decryptObj);
+        console.log("解密 json parse", decryptObj);
+        await withdraw_(decryptObj).then(result => {
+            ctx.body = result;
+        })
+    } catch (error) {
+        console.log("json error:", error);
+        ctx.body = utils.toJson(-1,null,error.toString());
+    }
+}
+
+async function withdraw_(obj) {
+    console.log("withdraw_", obj);
+    var log_obj = { ...obj }
+    var info = await moralis.queryCompanyInfoFromId(0);
+    log_obj.company_address_total_balance_before = await moralis.queryCollectBalance(info.user_address, obj.chain)
+    log_obj.company_public_key = info.user_address
+    logger.log('withdraw log', log_obj);
+    return new Promise((resolve) => {
+        moralis.withdraw(obj).then((result) => {
+            if (moralis.isTransferSucceed(result)) {
+                //提币日志上报
+                log_obj.results = result
+                log_obj.type = report.REPORT_TYPE.withdraw
+                //缓存当前交易的 gas 费用
+                if (result && log_obj.contractAddress) {
+                    var tr = moralis.getTransferGasFree('token', result)
+                    log_obj.withdrawTotalGasFee = tr.totalGasFree.toString()
+                } else {
+                    var tr = moralis.getTransferGasFree('native', ret)
+                    log_obj.withdrawTotalGasFee = tr.totalGasFree.toString()
+                }
+
+                // log_obj.receiver_address_total_balance_after = await queryCollectBalance(info.user_address, utils.getChainName(obj.chain))
+                //日志上报
+                report.logReport(log_obj)
+            }
+            resolve(result)
+        });
+    })
+}
+
 //出金
 async function withdraw(ctx) {
     if (ctx.request == null || ctx.request.body == null) {
@@ -103,7 +164,7 @@ async function withdraw(ctx) {
     var info = await moralis.queryCompanyInfoFromId(0);
     log_obj.company_address_total_balance_before = await moralis.queryCollectBalance(info.user_address, obj.chain)
     log_obj.company_public_key = info.user_address
-    logger.log('withdraw log', log_obj);
+    logger.log('withdraw log', log_obj,obj);
     await moralis.withdraw(obj).then((result) => {
         ctx.body = result;
         if (moralis.isTransferSucceed(result)) {
@@ -124,7 +185,6 @@ async function withdraw(ctx) {
             //日志上报
             report.logReport(log_obj)
         }
-
     });
 }
 
@@ -139,6 +199,8 @@ router.post('/getAllTotkenPrice', getAllTotkenPrice)
 // router.post('/transfer', transfer)
 //提现
 router.post('/withdraw', withdraw);
+//提现
+router.post('/withdrawV2', withdrawV2);
 //获取所有地址的所要消耗的最低提取费
 router.post('/getAllTokenWithdrawInfoLists', getAllTokenWithdrawInfoLists)