Advertisement
Guest User

Untitled

a guest
Aug 24th, 2019
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.68 KB | None | 0 0
  1. const fs = require('fs');
  2. var file = './Endgame-0.webm';
  3. const RippleAPI = require('ripple-lib').RippleAPI;
  4. var addy = 'address';
  5. var secret = 'secret';
  6.  
  7. // Ripple stuff
  8. const api = new RippleAPI({
  9. server: 'wss://s1.ripple.com'
  10. });
  11. api.on('error', (errorCode, errorMessage) => {
  12. console.log(errorCode + ': ' + errorMessage);
  13. });
  14. api.on('connected', () => {
  15. console.log('connected');
  16. });
  17. api.on('disconnected', (code) => {
  18. console.log('disconnected, code:', code);
  19. });
  20.  
  21. async function upload(file) {
  22. await api.connect();
  23. // Read file from disk and assemble the array in memory buffer
  24. fs.readFile(file, { encoding: 'base64' }, function (err, data ) {
  25. chunker(data);
  26. });
  27. }
  28.  
  29. function chunker(data){
  30. // Break into ordered 1024 chunks
  31. var re = new RegExp('.{1,960}', 'g');
  32. var array = data.match(re);
  33. datatochain(array);
  34. }
  35.  
  36. async function datatochain(array){
  37. // This is a master hash
  38. if (array.length == 1){
  39. await send(array[0], true).catch(error => console.error(error.stack));
  40. }
  41. else{
  42. var datablob = '';
  43. for(var i = 0; i < array.length; i++) {
  44. while (true) {
  45. var newdata = await send(array[i], false).catch(error => console.error(error.stack));
  46. if (typeof newdata !== 'undefined' && newdata !== 'undefined' && newdata.length === 64) {
  47. datablob += newdata;
  48. if(i== array.length-1) {
  49. chunker(datablob);
  50. break;
  51. }
  52. break;
  53. }
  54. }
  55. }
  56. }
  57. }
  58.  
  59. async function send(data, ismaster) {
  60. const payment = {
  61. source: {
  62. address: addy,
  63. maxAmount: {
  64. value: '0.000001',
  65. currency: 'XRP'
  66. }
  67. },
  68. destination: {
  69. address: 'rw2htZCsyJk8yNRYDxjiv9QFiZ2yqCQCPJ',
  70. amount: {
  71. value: '0.000001',
  72. currency: 'XRP'
  73. }
  74. },
  75. memos: [
  76. {
  77. data: data
  78. }
  79. ]
  80. };
  81. const prepared = await api.preparePayment(addy, payment, {
  82. maxLedgerVersionOffset: 5
  83. });
  84. const { signedTransaction } = api.sign(prepared.txJSON, secret);
  85. if (JSON.parse(prepared.txJSON).Fee > 12 ){
  86. return 'RETRY';
  87. }
  88. else{
  89. const res = await api.submit(signedTransaction);
  90. console.log(res.resultCode + ' ' + res.tx_json.Sequence);
  91. if (!res.tx_json.hash){
  92. return 'RETRY';
  93. }
  94. else if (res.resultCode != 'tesSUCCESS'){
  95. return 'RETRY';
  96. }
  97. else{
  98. if (ismaster){
  99. console.log('Done your master hash is: ' + res.tx_json.hash);
  100. await api.disconnect();
  101. }
  102. else{
  103. return res.tx_json.hash;
  104. }
  105. }
  106. }
  107. }
  108.  
  109. // Run logic
  110. upload(file).catch(error => console.error(error.stack));
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement