-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathDigiByteService.js
196 lines (163 loc) · 6.36 KB
/
DigiByteService.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
194
195
196
const DigiByte = require('digibyte')
const Request = require('request')
class DigiByteService
{
EXPLORER_URL = 'https://dgb.nownodes.io/api/v2/'
SAT_IN_DGB = 100000000
FEE_TO_SEND_DGB = 0.00005 * this.SAT_IN_DGB
constructor(nowNodesApiKey)
{
this.nowNodesApiKey = nowNodesApiKey
}
getNewWallet()
{
let wallet = DigiByte.PrivateKey()
return {
address : wallet.toLegacyAddress().toString(),
privateKey : wallet.toWIF()
}
}
getNowNodesOptions()
{
return {'headers': {'api-key': this.nowNodesApiKey}}
}
async getWalletBalance(address)
{
let url = this.EXPLORER_URL + 'address/' + address
let options = this.getNowNodesOptions()
let response = await this._sendRequestToUrl(url, options)
let resultJson = JSON.parse(response.toString())
return resultJson.balance
}
async getTransactionInfo(tx)
{
let url = this.EXPLORER_URL + 'tx/' + tx
let options = this.getNowNodesOptions()
let response = await this._sendRequestToUrl(url, options)
return JSON.parse(response.toString())
}
async getUnspentTransactionOutput(address)
{
let url = this.EXPLORER_URL + 'ut' + 'xo/' + address
let options = this.getNowNodesOptions()
let response = await this._sendRequestToUrl(url, options)
return JSON.parse(response.toString())
}
async sendFunds(sourcePrivateKeyWIF, sourceAddress, operations, callbackFunction, data)
{
let sourcePrivateKey = DigiByte.PrivateKey.fromWIF(sourcePrivateKeyWIF)
return new Promise((resolve, reject) => {
this.getWalletBalance(sourceAddress).then(balance => {
this.getUnspentTransactionOutput(sourceAddress).then(async (UTXOs) => {
if (!UTXOs.length) {
return reject({
'result' : 'error',
'message' : 'The source address has no unspent transactions'
});
}
let changePrivateKey = new DigiByte.PrivateKey()
let changeAddress = changePrivateKey.toAddress()
let transaction = this._makeTransactionFromFromUTXOs(UTXOs, sourceAddress)
this.satoshiLeft = parseInt(balance)
transaction = this._addTransactionToFromOperations(transaction, operations, sourceAddress)
if (data) {
transaction.addData(data)
}
transaction.change(changeAddress)
transaction.sign(sourcePrivateKey)
let txData = {
'source_private_key': sourcePrivateKeyWIF,
'source_address' : sourceAddress,
'change_private_key': changePrivateKey.toWIF(),
'change_address' : changeAddress,
'sent_amount' : balance
};
console.log(txData);
let result = await this._sendTransaction(transaction)
resolve(Object.assign(result, txData))
if (callbackFunction) {
callbackFunction('ok', 'deposited to pay fee for issue');
}
}, error => {
console.log('Error happened', error)
if (callbackFunction) {
callbackFunction('error', error)
}
reject(error)
})
})
}).then(result => {
return result;
})
}
async _sendTransaction(transaction)
{
let url = this.EXPLORER_URL + 'send' + 'tx/' + transaction.serialize(true)
let options = this.getNowNodesOptions()
let response = await this._sendRequestToUrl(url, options).catch(error => {
return error;
})
return JSON.parse(response.toString())
}
async _sendRequestToUrl(url, options = {})
{
return new Promise(function (resolve, reject) {
Request(url, options, function (error, res, body) {
if (!error && res.statusCode === 200) {
resolve(body);
} else {
reject(res.body);
}
});
});
}
_makeTransactionFromFromUTXOs(UTXOs, sourceAddress)
{
let transaction = new DigiByte.Transaction()
for (let index = 0; index < UTXOs.length; index++) {
let ut = {
address : sourceAddress,
txid : UTXOs[index]['txid'],
vout : UTXOs[index]['vout'],
ts : UTXOs[index]['height'],
scriptPubKey: UTXOs[index]['scriptPubKey'],
amount : parseFloat(UTXOs[index]['value']) / this.SAT_IN_DGB,
confirmationsFromCache: false
}
transaction.from(ut)
}
return transaction
}
_addTransactionToFromOperations(transaction, operations, sourceAddress)
{
for (let index in operations) {
if (!operations.hasOwnProperty(index)) {
continue
}
let line = operations[index]
if (!line.times) {
line.times = 1
}
for (let i = 0; i < line.times; i++) {
if (line.address === 'issueAddress') {
line.address = sourceAddress
}
let sum = parseInt(line.value * this.SAT_IN_DGB)
if (sum < 0) {
sum += this.satoshiLeft
}
transaction.to(line.address, sum)
this.satoshiLeft -= sum
if (this.satoshiLeft < this.FEE_TO_SEND_DGB) {
console.log('!!! No satoshi left in send-us: ' + this.satoshiLeft + '<' + this.FEE_TO_SEND_DGB)
}
}
}
if (this.satoshiLeft > this.FEE_TO_SEND_DGB) {
let sum = this.satoshiLeft - this.FEE_TO_SEND_DGB
transaction.to(sourceAddress, sum)
}
return transaction
}
}
exports.DigiByteService = DigiByteService