-
Notifications
You must be signed in to change notification settings - Fork 7
/
debugger.js
93 lines (73 loc) · 2.92 KB
/
debugger.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
const repl = require('repl');
const currentPath = __filename.substring(0,__filename.lastIndexOf('/'));
process.chdir(currentPath);
const messageUtils = require('./lib/serverMessages.js');
const _ = require('lodash');
const bch = require('bitcoincashjs-fork');
const shuffleIt = repl.start('cashshuffle debug > ');
shuffleIt.context.round;
try {
shuffleIt.context.round = require(currentPath+'/_failedShuffle.js');
}
catch(nope) {
console.log('No failed shuffle file found at', currentPath+'/_failedShuffle.js');
shuffleIt.context.round = {};
}
// shuffleIt.context.players = shuffleIt.context.round.players;
shuffleIt.context.inbox = shuffleIt.context.round.inbox;
shuffleIt.context.outbox = shuffleIt.context.round.outbox;
shuffleIt.context.me = _.find(shuffleIt.context.round.players, { isMe: true });
for (let onePlayer of shuffleIt.context.round.players) {
if (onePlayer.isMe) {
continue;
}
let somePlayer = {};
somePlayer['player'+onePlayer.playerNumber] = onePlayer;
_.extend(shuffleIt.context, somePlayer);
}
shuffleIt.context.bch = bch;
shuffleIt.context.Address = shuffleIt.context.bch.Address;
shuffleIt.context.PrivateKey = shuffleIt.context.bch.PrivateKey;
shuffleIt.context.PublicKey = shuffleIt.context.bch.PublicKey;
shuffleIt.context.Message = require('./lib/BetterMessage.js');
shuffleIt.context.msg = messageUtils;
shuffleIt.context.crypto = require('./lib/cryptoUtils.js');
shuffleIt.context._ = _;
shuffleIt.context.tools = {
crypto: require('./lib/cryptoUtils.js'),
coin: require('./lib/coinUtils.js'),
loadProtobuffMessageFromMailbox: function(someMailboxMessageObject) {
let messageBuffer = Buffer.from(someMailboxMessageObject.protobuffMessage.components.buffer, 'base64');
let decodedMessage = messageUtils.decodeAndClassify(messageBuffer);
delete decodedMessage.components;
let sender = _.find(shuffleIt.context.round.players, { session: decodedMessage.pruned.message.session });
return _.extend(decodedMessage, { sender: sender });
},
// Find a properly packed `Protocol Message` from
// somewhere deep inside a base64 encoded string.
findValidPackets: function(someBase64EncodedString) {
let messageBuffer = Buffer.from(someBase64EncodedString, 'base64');
let aintGood = true;
let indexCounter=0;
let packets;
let numTries = 0;
while (aintGood && numTries < someBase64EncodedString.length) {
numTries++;
try {
let messageMagic = messageBuffer.slice(indexCounter, indexCounter+8);
if (messageMagic.toString('hex') !== magic.toString('hex')) {
indexCounter += 1;
throw new Error('message_magic:'+(messageMagic.toString('hex')));
}
else {
packets = shuffleIt.context.crypto.PB.Packets.decode(messageBuffer.slice(indexCounter+12, )).toJSON();
aintGood = false;
}
}
catch(nope) {
}
}
console.log(JSON.stringify(packets,null,4));
return packets;
}
};