-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathduster.js
209 lines (193 loc) · 6.44 KB
/
duster.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
197
198
199
200
201
202
203
204
205
206
207
208
209
// Copyright (c) 2018-2019 The Genesis Network Developers
// Distributed under the MIT/X11 software license, see the accompanying
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
"use strict";
// Import some functionality
const bitcoin = require("bitcoin");
let Regex = require("regex");
// Constructor to get things set up
function Duster(currentWalletConfig) {
this.currentWalletConfig = currentWalletConfig;
this.baseWallet = new bitcoin.Client(this.currentWalletConfig);
}
// -------------------------- Helpers --------------------------------------------------------
var sortByAmount = function(state) {
return new Promise(function(resolve, reject) {
// Custom compare by amount
function compare(a, b) {
if (a.amount < b.amount) return -1;
if (a.amount > b.amount) return 1;
return 0;
}
// sort the values from smallest to largest
state.txList.sort(compare);
if (state.txList) {
resolve(state);
} else {
reject(Error("Unable to sort"));
}
});
};
var makeBatches = function(state) {
return new Promise(function(resolve, reject) {
state.inputBatches = [];
state.inputBatches[0] = [];
var elementCounter = 0;
var batchTracker = 0;
for (let index = 0; index < state.txList.length; index++) {
const element = state.txList[index];
if (
element.address === state.sourceAddress &&
element.amount < state.maximumInputAmount &&
element.confirmations > state.minimumConfirmations
) {
if (elementCounter == state.batchSize) {
// Init the next batch
elementCounter = 0;
batchTracker++;
state.inputBatches[batchTracker] = [];
}
// Add the element to this batch
state.inputBatches[batchTracker].push(element);
elementCounter++;
}
}
if (state.inputBatches) {
resolve(state);
} else {
reject(Error("Unable to create batches"));
}
});
};
var consolidateBatch = function(batchState) {
return new Promise(function(resolve, reject) {
batchState.items.forEach(inputTx => {
batchState.batchValue += inputTx.amount;
batchState.candidates.push({
txid: inputTx.txid,
vout: inputTx.vout
});
batchState.rawItems.push({
txid: inputTx.txid,
vout: inputTx.vout,
scriptPubKey: inputTx.scriptPubKey,
redeemScript: inputTx.redeemScript,
amount: inputTx.amount
});
});
let payoutAmount =
Math.round((batchState.batchValue - batchState.batchTxFee) * 1000) / 1000;
// console.log(payoutAmount + '/' + batchState.batchValue + '/' + batchState.batchTxFee);
batchState.payment[batchState.sourceAddress] = payoutAmount;
if (batchState) {
resolve(batchState);
} else {
reject(Error("Unable to sort"));
}
});
};
var createBatchTransaction = function(batchState) {
return new Promise(function(resolve, reject) {
batchState.baseWallet.createRawTransaction(
batchState.candidates,
batchState.payment,
0,
function(err, rawtx) {
if (err) {
console.log(batchState.candidates);
console.log(batchState.payment);
reject(Error(err));
} else {
let rawSignPrivKeys = batchState.privateKeys;
batchState.baseWallet.signRawTransaction(
rawtx,
batchState.rawItems,
rawSignPrivKeys,
function(signErr, rawSignResult) {
if (signErr) {
console.log(batchState.rawItems[0]);
reject(Error(signErr));
} else {
//resolve(rawSignResult.complete);
//resolve(rawSignResult.hex);
batchState.baseWallet.sendRawTransaction(
rawSignResult.hex,
function(submitErr, submitResult) {
if (submitErr) {
reject(Error(submitErr));
} else {
resolve(submitResult);
}
}
);
}
}
);
}
}
);
});
};
// -------------------------- Exported Functions ---------------------------------------------
Duster.prototype.ProcessWalletRun = function(currentRunState) {
// Dumb, but needed
var that = this;
return new Promise(function(resolve, reject) {
// For now, only process the first wallet... untill I figure out concurrency
var currentRun = currentRunState.runconfig;
if (currentRun.active) {
var runConfig = currentRun.config;
that.baseWallet.listUnspent(runConfig.minimum_confirmations, function(
err,
unspent
) {
if (err) {
reject(err);
} else {
console.log("Total Inputs: " + unspent.length);
var state = {
txList: unspent,
batchSize: runConfig.batch_size,
sourceAddress: runConfig.address,
privateKeys: runConfig.private_keys,
maximumInputAmount: runConfig.maximum_input_amount,
minimumConfirmations: runConfig.minimum_confirmations,
batchTxFee: runConfig.batch_tx_fee
};
sortByAmount(state)
.then(makeBatches)
.then(function(batchedState) {
var inputBatches = batchedState.inputBatches;
inputBatches.forEach(batch => {
var batchState = {
sourceAddress: batchedState.sourceAddress,
privateKeys: batchedState.privateKeys,
batchTxFee: batchedState.batchTxFee,
batchValue: 0,
candidates: [],
rawItems: [],
payment: {},
items: batch,
baseWallet: that.baseWallet
};
batchState.payment[batchState.sourceAddress] = 0;
consolidateBatch(batchState)
.then(createBatchTransaction)
.then(function(results) {
resolve(results);
})
.catch(function(rejection) {
reject(rejection);
});
});
});
}
});
} else {
// Being inactive is not an error...
resolve("Run configuration is inactive");
}
});
};
// -------------------------- Exports --------------------------------------------------------
exports.Duster = Duster;