Skip to content

Commit

Permalink
1 file modified
Browse files Browse the repository at this point in the history
  • Loading branch information
ankurp committed May 21, 2021
1 parent c623c87 commit fe2139f
Showing 1 changed file with 41 additions and 37 deletions.
78 changes: 41 additions & 37 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,43 +13,47 @@ const {
} = sdk;

// function to encode file data to base64 encoded string
async function base64Encode(url) {
const bitmap = await fetch(url);
function base64Encode(filePath) {
const bitmap = fs.readFileSync(filePath);
return new Buffer.from(bitmap).toString('base64');
}

const data = base64Encode('');

// const server = new Server('https://horizon-testnet.stellar.org');
// const sourceKey = Keypair.fromSecret(
// 'SBELXHH4YQRLYPCETDMRGTHJGOGTJHDZRCLJM34OT3J23DERVQW542FZ'
// );

// await fetch(`https://friendbot.stellar.org?addr=${sourceKey.publicKey()}`);

// const sourceAccount = await server.loadAccount(sourceKey.publicKey());
// const transactionBuilder = new TransactionBuilder(sourceAccount, {
// fee: BASE_FEE * 100,
// networkPassphrase: Networks.TESTNET
// });

// const chunksOfData = data.split('');

// for (let j = 0; chunksOfData.length > 0; j++) {
// transactionBuilder.addOperation(
// Operation.manageData({
// name: ('00' + j).slice(-2) + chunksOfData.splice(0, 62).join(''),
// value: chunksOfData.splice(0, 64).join('')
// })
// );
// }

// const transaction = transactionBuilder.setTimeout(TimeoutInfinite).build();
// transaction.sign(sourceKey);

// try {
// const result = await server.submitTransaction(transaction);
// console.log('Success! Results:', result);
// } catch (e) {
// console.error('ERROR: ', e);
// }
const data = base64Encode(
'./GCEE5H3RI2MFP4UQ4NHFKLGTIHILWA775AM7KTLU5HUBSLOBJN7M4RSL.png'
);

const server = new Server('https://horizon-testnet.stellar.org');
const sourceKey = Keypair.random();

console.log('Public Key: ', sourceKey.publicKey());

(async () => {
await fetch(`https://friendbot.stellar.org?addr=${sourceKey.publicKey()}`);

const sourceAccount = await server.loadAccount(sourceKey.publicKey());
const transactionBuilder = new TransactionBuilder(sourceAccount, {
fee: BASE_FEE * 100,
networkPassphrase: Networks.TESTNET
});

const chunksOfData = data.split('');

for (let j = 0; chunksOfData.length > 0; j++) {
transactionBuilder.addOperation(
Operation.manageData({
name: ('00' + j).slice(-2) + chunksOfData.splice(0, 62).join(''),
value: chunksOfData.splice(0, 64).join('')
})
);
}

const transaction = transactionBuilder.setTimeout(TimeoutInfinite).build();
transaction.sign(sourceKey);

try {
const result = await server.submitTransaction(transaction);
console.log('Success! Results:', result);
} catch (e) {
console.error('ERROR: ', e);
}
})();

0 comments on commit fe2139f

Please sign in to comment.