-
Notifications
You must be signed in to change notification settings - Fork 12
/
coinjoin.js
346 lines (301 loc) · 9.66 KB
/
coinjoin.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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
'use strict';
if (typeof module === 'object' && typeof define !== 'function') {
var define = function (factory) {
module.exports = factory(require, exports, module);
};
}
define(function (require, exports, module) {
var Bitcoin = require('bitcoinjs-lib');
/*
* CoinJoin Class
* @constructor
*/
function CoinJoin(core, role, state, tx, myAmount, fee, peer) {
this.core = core;
this.state = state;
this.role = role;
this.myTx = tx;
this.myAmount = myAmount;
this.fee = fee;
// Peer set initially for guest
this.peer = peer;
}
/**
* Randomize array element order in-place.
* Using Fisher-Yates shuffle algorithm.
*/
CoinJoin.prototype.shuffleArray = function(array) {
for (var i = array.length - 1; i > 0; i--) {
var j = Math.floor(Math.random() * (i + 1));
var temp = array[i];
array[i] = array[j];
array[j] = temp;
}
return array;
}
/*
* Randomize join outputs and inputs
* Happens on the first fullfill by the initiator, and first sign by the guest.
*/
CoinJoin.prototype.randomize = function(tx) {
var self = this;
tx = tx.clone();
var newTx = new Bitcoin.Transaction();
var stealth = [];
// Add non stealth outputs, and keep a matrix of where to put them after
tx.outs.forEach(function(anOut, idx) {
if (self.isStealth(anOut) && tx.outs.length > idx) {
// Save an array with the nonce output and then the related output
stealth.push([anOut, tx.outs[idx+1]]);
} else {
// this will just push the same output into newTx.outs
newTx.addOutput(tx.outs[idx].script, tx.outs[idx].value);
}
});
// Copy inputs since no need to keep any order
newTx.ins = tx.ins.slice(0);
// Now shuffle the transaction
this.shuffleArray(newTx.ins);
this.shuffleArray(newTx.outs);
// Now re-insert (possibly) stealth information
stealth.forEach(function(nonces) {
var origOut = nonces[1];
var found = newTx.outs.filter(function(newOut) {
return (origOut.script.toHex() === newOut.script.toHex()) && (origOut.value === newOut.value);
});
if (found.length === 1) {
var index = newTx.outs.indexOf(found[0]);
// add the output back
newTx.addOutput(nonces[0].script, nonces[0].value);
// now set it in place
newTx.outs.splice(index, 0, newTx.outs.pop());
} else {
throw Error("Cant add output");
}
});
return newTx;
}
/*
* CoinJoin State machine once paired
* 1st message guest -> [initiator]
*/
CoinJoin.prototype.fullfill = function(msg, peer) {
// Check there is one output like we want to join
var amount = this.myAmount;
var remoteTx = Bitcoin.Transaction.fromHex(msg.tx);
var isOk = false;
remoteTx.outs.forEach(function(anOut) {
if (anOut.value == amount) {
isOk = true;
}
});
if (!isOk) {
console.log("no output found with the right value");
return;
}
// Now check there are inputs with enough funds
isOk = false;
// Now add our inputs and outputs after the ones from guest
var myTx = this.myTx;
myTx.ins.forEach(function(anIn) {
remoteTx.addInput(anIn.hash, anIn.index);
});
myTx.outs.forEach(function(anOut) {
remoteTx.addOutput(anOut.script, anOut.value);
});
// Randomize inputs and outputs
remoteTx = this.randomize(remoteTx);
// Check the original inputs and outputs are there
if (!this.checkMyInputsOutputs(this.myTx, remoteTx)) {
this.cancel();
return;
}
// Save tx
this.tx = remoteTx;
this.state = 'fullfilled';
// Lock peer
this.peer = peer;
return remoteTx;
};
/*
* 1st message initiator -> [guest]
*/
CoinJoin.prototype.sign = function(msg, peer) {
if (peer != this.peer) {
return;
}
var remoteTx = Bitcoin.Transaction.fromHex(msg.tx);
// Randomize inputs and outputs
remoteTx = this.randomize(remoteTx);
// Check the original inputs and outputs are there
if (!this.checkMyInputsOutputs(this.myTx, remoteTx)) {
this.cancel();
return;
}
// Needs signing of inputs
this.tx = remoteTx;
this.state = 'sign';
return remoteTx;
};
/*
* Add signatures manually
* tx: Transaction with user inputs signed
*/
CoinJoin.prototype.addSignatures = function(tx) {
// Save tx
// TODO: maybe check signatures, but they come from the application here
this.tx = tx;
if (this.role == 'initiator') {
this.state = 'finished';
} else {
this.state = 'signed';
}
return tx;
}
/*
* 2nd message guest -> [initiator]
*/
CoinJoin.prototype.finishInitiator = function(msg, peer) {
if (peer != this.peer) {
return;
}
var myTx = this.tx;
var remoteTx = Bitcoin.Transaction.fromHex(msg.tx);
// Check no new inputs or outputs where added
if (!this.checkInputsOutputs(myTx, remoteTx)) {
this.cancel();
return;
}
// Check the guest signed
// Now sign our input(s) against the outputs
this.tx = remoteTx;
this.state = 'sign';
return remoteTx;
};
/*
* 2nd message initiator -> [guest]
*/
CoinJoin.prototype.finishGuest = function(msg, peer) {
if (peer != this.peer) {
return;
}
var remoteTx = Bitcoin.Transaction.fromHex(msg.tx);
// Check no new inputs or outputs where added
if (!this.checkInputsOutputs(this.tx, remoteTx)) {
this.cancel();
return;
}
// Check our signatures are there
// Check the inititator signed
// We are done here...
// Save tx
this.state = 'finished';
this.tx = remoteTx;
return remoteTx;
};
/*
* Process a message for an ongoing CoinJoin
*/
CoinJoin.prototype.process = function(msg, peer) {
switch (this.state) {
case 'announce':
// 1. If initiator, comes with new input and outputs from guest
return this.fullfill(msg, peer);
case 'accepted':
// 2. If guest, comes with full tx, check and sign
return this.sign(msg, peer);
case 'fullfilled':
// 3. Initiator finally signs his part
return this.finishInitiator(msg, peer);
case 'signed':
// 4. Guest getting the final transaction
return this.finishGuest(msg, peer);
}
};
CoinJoin.prototype.cancel = function() {
if (this.role == 'initiator') {
// Back to announce state
this.state = 'announce';
}
else if (this.role == 'guest') {
this.state = 'cancelled';
}
};
/*
* Process a message finishing a coinjoin conversation
*/
CoinJoin.prototype.kill = function(body, peer) {
// Only allow finish messages from our peer
if (peer != this.peer) {
return;
}
switch(this.state) {
case 'accepted':
case 'fullfilled':
case 'signed':
this.cancel();
break;
case 'finished':
case 'announce':
default:
// do nothing
break;
}
};
/*
* Helper functions
*/
CoinJoin.prototype.isStealth = function(anOut) {
// Value must be 0, size 38, first byte OP_RETURN and there must be an output after this one
return (anOut.value == 0 && anOut.script.toBuffer().length >= 38 && anOut.script.toBuffer()[0] == Bitcoin.opcodes.OP_RETURN);
};
CoinJoin.prototype.checkInputsOutputs = function(origTx, newTx) {
var isValid = true;
if (origTx.ins.length != newTx.ins.length) return false;
if (origTx.outs.length != newTx.outs.length) return false;
isValid = this.checkMyInputsOutputs(origTx, newTx);
return isValid;
};
CoinJoin.prototype.checkMyInputsOutputs = function(origTx, newTx) {
for(var i=0; i<origTx.ins.length; i++) {
// TODO: should check the scripts too
var origInP = origTx.ins[i];
var found = newTx.ins.filter(function(newIn) {
return (origInP.hash.toString('hex') == newIn.hash.toString('hex')) && (parseInt(origInP.index) == parseInt(newIn.index));
});
if (found.length != 1) return false;
}
for(var i=0; i<origTx.outs.length; i++) {
var origOut = origTx.outs[i];
var found = newTx.outs.filter(function(newOut) {
return (origOut.script.toHex() === newOut.script.toHex()) && (origOut.value === newOut.value);
});
if (found.length != 1) return false;
// Check stealth is ordered
var isStealth = this.isStealth(origOut) && origTx.outs.length > i;
if (isStealth) {
var j = newTx.outs.indexOf(found[0]);
if (!origTx.outs[i+1] || !newTx.outs[j+1] || origTx.outs[i+1].script.toHex() != newTx.outs[j+1].script.toHex()) {
console.log("unordered stealth");
return false;
}
}
}
return true;
};
CoinJoin.Protocol = {
packMessage: function(type, data) {
var request = {}
request['type'] = type;
request['body'] = data;
return request;
},
CoinJoinMsg: function(id, tx) {
var data = {};
data['id'] = id;
data['tx'] = tx;
return CoinJoin.Protocol.packMessage('CoinJoin', data);
}
}
return CoinJoin;
});