-
Notifications
You must be signed in to change notification settings - Fork 1
/
controller.js
193 lines (164 loc) · 5.32 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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
const { DigiByteService } = require('./DigiByteService.js')
require('dotenv').config()
class Controller
{
ROUTES = {
'test-connect' : 'actionTestConnect',
'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) {
return finishFunction({
'status' : 'error',
'message' : 'Please set up environment'
})
}
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) {
return finishFunction({
'status' : 'error',
'message' : 'address is not specified'
})
}
this.DigiByteServiceInstance.getWalletBalance(address).then(
(result) => {
finishFunction({
'status' : 'ok',
'balance' : result
})
}
)
}
actionGetUnspentTxo(params, finishFunction)
{
let address = params['address']
if (!address) {
return finishFunction({
'status' : 'error',
'message' : 'address is not specified'
})
}
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']
let paymentFee = this.DigiByteServiceInstance.FEE_TO_SEND_DGB / this.DigiByteServiceInstance.SAT_IN_DGB
let operations;
let operationInfoByIndex = function (params, index) {
let indexBase = 'operations[' + index + ']';
let address = paramsArray[indexBase + '[addr]'];
if (!address) {
return null;
}
let value = paramsArray[indexBase + '[value]'];
let times = paramsArray[indexBase + '[times]'];
if (!times) {
times = 1;
}
return {
address : address,
value : value,
times : times
}
}
let info = operationInfoByIndex(paramsArray, 0);
if (info) {
operations = []
let index = 0
while (info) {
operations.push(info)
index ++
info = operationInfoByIndex(paramsArray, index)
}
} else {
let amount = parseFloat(paramsArray['overallSum']) - paymentFee
let address = paramsArray['destinationAddress']
if (!address) {
address = process.env.ADMIN_ADDRESS;
}
operations = [{ address: address, value: amount, times: 1 }]
}
await this.DigiByteServiceInstance.sendFunds(sourcePrivateKey, sourceAddress, operations, console.log).then(
(result) => {
finishFunction(result)
}
)
}
actionGetTxInfo(params, finishFunction)
{
let tx = params['tx'];
if (!tx) {
return finishFunction({
'status' : 'error',
'message' : 'tx is not specified'
})
}
this.DigiByteServiceInstance.getTransactionInfo(tx).then(
(result) => {
finishFunction(result)
}
);
}
actionTestConnect(params, finishFunction)
{
finishFunction({
'status' : 'ok',
})
}
}
exports.Controller = Controller