-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathtestShuffle.js
143 lines (107 loc) · 3.72 KB
/
testShuffle.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
const ShuffleClient = require('../cashshufflejs-web');
const JsonWallet = require('./JsonWallet.js');
const _ = require('lodash');
const repl = require('repl');
const shuffleIt = repl.start('cashshuffle > ');
const delay = ms => new Promise(resolve => setTimeout(resolve, ms));
const myWallet = new JsonWallet({
file: './test_json_wallet.js'
});
// Unfreeze any frozen addresses
myWallet.unfreezeAddresses( _.map(myWallet.addresses, 'cashAddress') );
// Load up our on-disk HD wallet
shuffleIt.context.wallet = myWallet;
// The two functions below provide us a way
// of plugging the `ShuffleClient` into our
// bitcoin wallet software. They are called
// by the client when new payment addresses
// are needed during shuffle operations.
// This function should return a single new change
// address when called. We pass this function as
// a parameter to our `ShuffleClient` instance so
// that it may fetch change addresses as needed.
const newChangeAddressFromWallet = function() {
return myWallet.fresh.change();
};
// Ditto but for shuffled coins. The on-disk wallet
// is using a dedicated HD path for all shuffled coins.
// I think you should too!
const newAddressForShuffledCoin = function() {
return myWallet.fresh.shuffle();
};
const grabCoinToShuffle = async function() {
let oneCoin;
while (!oneCoin) {
oneCoin = _.find(_.shuffle(myWallet.coins.slice(0,8)), {
// oneCoin = _.find(myWallet.coins.reverse(), {
frozen: false
});
if (oneCoin) {
myWallet.freezeAddresses(oneCoin.cashAddress);
continue;
}
else {
// console.log('...');
await delay(750);
}
}
return oneCoin;
};
const addClientToShuffle = async function(clientNumber) {
let clientName = 'client'+clientNumber;
console.log(`Adding ${clientName} to the shuffle`);
shuffleIt.context[clientName] = new ShuffleClient({
coins: [ await grabCoinToShuffle() ],
hooks: {
change: newChangeAddressFromWallet,
shuffled: newAddressForShuffledCoin
},
protocolVersion: 300,
maxShuffleRounds: 1,
// Disable automatically joining shuffle rounds
// once a connection with the server is established
disableAutoShuffle: false,
serverStatsUri: 'http://localhost:8080/stats'
});
// This event is emitted only when a successful shuffle round occurs.
// Currently all change is re-added to the client's pool of unshuffled
// coins (but in the new address returned by the HD wallet hook) so
// they too can be shuffled. Here you would do things like un-freeze
// shuffled coins, update UI's, etc.
shuffleIt.context[clientName].on('shuffle', async(shuffleRound) => {
console.log(`Coin ${ shuffleRound.coin.txid}:${ shuffleRound.coin.vout } has been successfully shuffled!`);
let coinsToUnfreeze = _.map([ shuffleRound.change, shuffleRound.shuffled ], 'cashAddress');
// Just a random delay to more equally distribute
// the load on the bitcoin.com servers.
await delay(Math.random()*3000+570);
try {
await myWallet.updateAddresses()
}
catch(nope) {
console.log('Somethings gone wrong', nope);
process.exit();
}
myWallet.unfreezeAddresses( coinsToUnfreeze );
shuffleIt.context[clientName].addUnshuffledCoins([ await grabCoinToShuffle() ]);
});
};
myWallet
.updateAddresses()
.catch((someError) => {
console.log('Error building coin info from wallet:', someError);
throw(someError);
})
.then(async (updatedWallet) => {
let numberOfClients = 1;
while (numberOfClients > 0) {
try {
await addClientToShuffle(numberOfClients)
}
catch(nope) {
console.log('Cannot add new client to shuffle:', nope);
process.exit();
}
await delay(Math.random()*1000+500);
numberOfClients--;
}
});