Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- const fs = require('fs');
- var file = './Endgame-0.webm';
- const RippleAPI = require('ripple-lib').RippleAPI;
- var addy = 'address';
- var secret = 'secret';
- // Ripple stuff
- const api = new RippleAPI({
- server: 'wss://s1.ripple.com'
- });
- api.on('error', (errorCode, errorMessage) => {
- console.log(errorCode + ': ' + errorMessage);
- });
- api.on('connected', () => {
- console.log('connected');
- });
- api.on('disconnected', (code) => {
- console.log('disconnected, code:', code);
- });
- async function upload(file) {
- await api.connect();
- // Read file from disk and assemble the array in memory buffer
- fs.readFile(file, { encoding: 'base64' }, function (err, data ) {
- chunker(data);
- });
- }
- function chunker(data){
- // Break into ordered 1024 chunks
- var re = new RegExp('.{1,960}', 'g');
- var array = data.match(re);
- datatochain(array);
- }
- async function datatochain(array){
- // This is a master hash
- if (array.length == 1){
- await send(array[0], true).catch(error => console.error(error.stack));
- }
- else{
- var datablob = '';
- for(var i = 0; i < array.length; i++) {
- while (true) {
- var newdata = await send(array[i], false).catch(error => console.error(error.stack));
- if (typeof newdata !== 'undefined' && newdata !== 'undefined' && newdata.length === 64) {
- datablob += newdata;
- if(i== array.length-1) {
- chunker(datablob);
- break;
- }
- break;
- }
- }
- }
- }
- }
- async function send(data, ismaster) {
- const payment = {
- source: {
- address: addy,
- maxAmount: {
- value: '0.000001',
- currency: 'XRP'
- }
- },
- destination: {
- address: 'rw2htZCsyJk8yNRYDxjiv9QFiZ2yqCQCPJ',
- amount: {
- value: '0.000001',
- currency: 'XRP'
- }
- },
- memos: [
- {
- data: data
- }
- ]
- };
- const prepared = await api.preparePayment(addy, payment, {
- maxLedgerVersionOffset: 5
- });
- const { signedTransaction } = api.sign(prepared.txJSON, secret);
- if (JSON.parse(prepared.txJSON).Fee > 12 ){
- return 'RETRY';
- }
- else{
- const res = await api.submit(signedTransaction);
- console.log(res.resultCode + ' ' + res.tx_json.Sequence);
- if (!res.tx_json.hash){
- return 'RETRY';
- }
- else if (res.resultCode != 'tesSUCCESS'){
- return 'RETRY';
- }
- else{
- if (ismaster){
- console.log('Done your master hash is: ' + res.tx_json.hash);
- await api.disconnect();
- }
- else{
- return res.tx_json.hash;
- }
- }
- }
- }
- // Run logic
- upload(file).catch(error => console.error(error.stack));
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement