forked from cept73/digibyte-nownode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcontroller.js
136 lines (113 loc) · 3.78 KB
/
controller.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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
const { DigiByteService } = require('./DigiByteService.js')
require('dotenv').config()
class Controller
{
ROUTES = {
'generate-wallet' : 'actionGenerateWallet',
'check-wallet' : 'actionCheckWallet',
'get-unspent-txo' : 'actionGetUnspentTxo',
'send-funds' : 'actionSendFunds',
'get-tx-info' : 'actionGetTxInfo'
}
DigiByteServiceInstance = new DigiByteService(process.env.NOWNODES_API_KEY)
run(jsonRequest, finishFunction)
{
if (!process.env.NOWNODES_API_KEY) {
finishFunction({
'status' : 'error',
'message' : 'Please set up environment'
})
return
}
let cmd = jsonRequest.url.substring(1)
let params = this._detectParamsFromRequest(jsonRequest)
const paramsList = params ? ' with params: ' + JSON.stringify(params) : ''
console.log('Run command: ' + cmd + paramsList)
if (cmd in this.ROUTES) {
let action = this.ROUTES[cmd]
this[action](params, finishFunction)
return
}
finishFunction({
'status' : 'error',
'message' : 'Unknown command'
})
}
_detectParamsFromRequest(jsonRequest)
{
let params
try {
// Thanks to https://stackoverflow.com/a/8649003
params = !jsonRequest.body ? '[]' : JSON.parse(
`{"${decodeURI(jsonRequest.body)
.replace(/"/g, '\\"')
.replace(/&/g, '","')
.replace(/=/g, '":"')}"}`
)
}
catch (e) {
params = jsonRequest.body
}
if (params === '[]') {
params = null
}
return params
}
actionGenerateWallet(params, finishFunction)
{
let newWalletInfo = this.DigiByteServiceInstance.getNewWallet()
newWalletInfo['status'] = 'ok'
finishFunction(newWalletInfo)
}
actionCheckWallet(params, finishFunction)
{
let address = params['address'];
if (!address) {
finishFunction({
'status' : 'error',
'message' : 'Unknown command'
})
return;
}
this.DigiByteServiceInstance.getWalletBalance(address).then(
(result) => {
finishFunction({
'status' : 'ok',
'balance' : result
})
}
)
}
actionGetUnspentTxo(params, finishFunction)
{
let address = params['address']
this.DigiByteServiceInstance.getUnspentTransactionOutput(address).then(
(result) => {
finishFunction(result)
}
);
}
async actionSendFunds(params, finishFunction)
{
const paramsArray = params // JSON.parse(params)
const sourceAddress = paramsArray['address']
const sourcePrivateKey = paramsArray['privateKey']
const toAddress = process.env.ADMIN_ADDRESS
const amount = parseFloat(process.env.AMOUNT_TO_DEPOSIT) - (this.DigiByteServiceInstance.FEE_TO_SEND_DGB / this.DigiByteServiceInstance.SAT_IN_DGB)
let operations = [{ address: toAddress, value: amount, times: 1 }]
await this.DigiByteServiceInstance.sendFunds(sourcePrivateKey, sourceAddress, operations, console.log).then(
(result) => {
finishFunction(result)
}
)
}
actionGetTxInfo(params, finishFunction)
{
this.DigiByteServiceInstance.getTransactionInfo(params['tx']).then(
(result) => {
finishFunction(result)
}
);
}
}
exports.Controller = Controller