-
Notifications
You must be signed in to change notification settings - Fork 0
/
scp_xs.js
63 lines (51 loc) · 2.24 KB
/
scp_xs.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
// Distributed under MS-RSL license: see /LICENSE for terms. Copyright 2019-2021 Dominic Morris.
//
// scp_xs -- exchange service(s)
//
'use strict';
const CryptoJS = require("crypto-js");
const config = require('./config');
const sql = require('mssql');
module.exports = {
changelly_sign: function (req, res) { // sign changelly api call
if (!req.body || !req.body.rpc_params) {
res.status(400).send( { msg: "bad params (1)" } ); return;
}
const rpc_params_json = req.body.rpc_params;
if (rpc_params_json.length == 0) {
res.status(400).send( { msg: "bad params (2)" } ); return;
}
// string json to obj
const o_rpc_params = JSON.parse(rpc_params_json);
if (!o_rpc_params) {
res.status(400).send( { msg: "bad params (3)" } ); return;
}
//console.log("changelly_sign - o_rpc_params=", o_rpc_params);
// save
if (o_rpc_params.method === 'createFixTransaction' || o_rpc_params.method === 'createTransaction') {
const p = o_rpc_params.params
const from = p.from
const to = p.to
const address = p.address
const amount = p.amount
const rateId = p.rateId
scp_sql_pool.request()
.input('from', sql.NVarChar, `${from}`)
.input('to', sql.NVarChar, `${to}`)
.input('address', sql.NVarChar, `${address}`)
.input('amount', sql.Decimal(14,8), `${amount}`)
.input('rateId', sql.NVarChar, `${rateId}`)
.query(`INSERT INTO [_scpx_xs_tx] VALUES (GETUTCDATE(), @from, @to, @address, @amount, @rateId, 'CHANGELLY')`)
.then((result) => {
//console.log(`changelly_sign: - xs_tx save ok (amount=${amount})`, result.rowsAffected);
}).catch(err => {
console.warn(`## changelly_sign - SQL failed: ${err.message}`, err);
})
}
// sign
const sign = CryptoJS.HmacSHA512(JSON.stringify(o_rpc_params), config.get("changelly_api_secret"));
const sig = sign.toString()
console.log(`$$ changelly_sign: rpc_params_json=(${rpc_params_json}) - OK 200`);
res.status(200).send({ res: "ok", data: sig });
},
};