testShuffle.js 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. /* Import core modules. */
  2. const debug = require('debug')('cashshuffle:test')
  3. /* Import local modules. */
  4. const ShuffleClient = require('./src/libs/shuffle/ShuffleClient.js')
  5. // const testCoins = require('./testCoins')
  6. /* Initialize Shuffle Manager. */
  7. let shuffleManager = null
  8. /**
  9. * Delay (Execution)
  10. */
  11. const delay = (ms) => new Promise(resolve => setTimeout(resolve, ms))
  12. /**
  13. * Start (CashShuffle)
  14. */
  15. const start = async function () {
  16. debug(`Starting CashShuffle test...`)
  17. /* Initialize new shuffle client. */
  18. shuffleManager = new ShuffleClient({
  19. coins: [ require('./testCoins').source ],
  20. hooks: {
  21. change: require('./testCoins').change, // NOTE: This is a function.
  22. shuffled: require('./testCoins').target, // NOTE: This is a function.
  23. },
  24. protocolVersion: 300,
  25. maxShuffleRounds: 1,
  26. // Disable automatically joining shuffle rounds
  27. // once a connection with the server is established
  28. disableAutoShuffle: false,
  29. // serverUri: 'https://shuffle.servo.cash:1337',
  30. // serverUri: 'wss://shuffle.servo.cash:1338',
  31. serverStatsUri: 'https://shuffle.servo.cash:8080/stats'
  32. // serverStatsUri: 'http://qvzl7zjviaw6532kn5onvlqluwxrlwhjpybsya4fw33ggvhzrak4z3qd.onion:8081/stats'
  33. })
  34. /**
  35. * Shuffle Success
  36. *
  37. * This event is emitted only when a successful shuffle round occurs.
  38. * Currently all change is re-added to the client's pool of unshuffled
  39. * coins (but in the new address returned by the HD wallet hook) so
  40. * they too can be shuffled.
  41. *
  42. * NOTE: Here you would do things like un-freeze shuffled coins,
  43. * update UI's, etc.
  44. */
  45. shuffleManager.on('shuffle', async (shuffleRound) => {
  46. debug(`Coin ${shuffleRound.coin.txid}:${shuffleRound.coin.vout} has been successfully shuffled!`)
  47. // Just a random delay to more equally distribute
  48. // the load on the bitcoin.com servers.
  49. await delay(Math.random() * 3000 + 570)
  50. /* Quit application. */
  51. process.exit()
  52. })
  53. }
  54. /* Start test. */
  55. start()