Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- var StellarSdk = require('stellar-sdk');
- var server = new StellarSdk.Server('http://localhost:8000', { allowHttp: true });
- StellarSdk.Network.use(new StellarSdk.Network("Standalone Network ; February 2017"));
- var pairMaster = StellarSdk.Keypair.master();
- const express = require('express')
- const app = express()
- app.get('/newaccount', (request, response) => {
- var pairNewAccount = StellarSdk.Keypair.random();
- server.loadAccount(pairMaster.publicKey())
- .then((account) => {
- var transaction = new StellarSdk.TransactionBuilder(account)
- .addOperation(StellarSdk.Operation.createAccount({
- destination: pairNewAccount.publicKey(),
- startingBalance: '500',
- }))
- .build();
- transaction.sign(StellarSdk.Keypair.fromSecret(pairMaster.secret()));
- server.submitTransaction(transaction)
- .then(() => {
- response.send('Account created with the publicKey '
- + pairNewAccount.publicKey() + ' and secret key '
- + pairNewAccount.secret())
- })
- .catch((err) => {
- console.log(err);
- response.send('An error ocurred!')
- });
- })
- .catch((err) => {
- console.error(err);
- response.send('An error ocurred!')
- });
- })
- app.get('/payment/:secret/:to/:amount', (request, response) => {
- var pairUser = StellarSdk.Keypair.fromSecret(request.params.secret);
- var pairTo = StellarSdk.Keypair.fromPublicKey(request.params.to);
- server.loadAccount(pairUser.publicKey())
- .then((account) => {
- var transaction = new StellarSdk.TransactionBuilder(account)
- .addOperation(StellarSdk.Operation.payment({
- destination: pairTo.publicKey(),
- asset: StellarSdk.Asset.native(),
- amount: request.params.amount,
- }))
- .build();
- transaction.sign(StellarSdk.Keypair.fromSecret(pairUser.secret()));
- server.submitTransaction(transaction)
- .then(() => {
- response.send(request.params.amount + ' lumens sent to ' + pairTo.publicKey())
- })
- .catch((err) => {
- console.log(err);
- response.send('An error ocurred!')
- });
- })
- .catch((err) => {
- console.error(err);
- response.send('An error ocurred!')
- });
- });
- app.get('/buyasset/:name/:amount/:to', (request, response) => {
- // Keys for accounts to issue and receive the new asset
- var issuingKeys = StellarSdk.Keypair.fromSecret(pairMaster.secret());
- var receivingKeys = StellarSdk.Keypair.fromSecret(request.params.to);
- var assetName = request.params.name;
- var amuntToBuy = parseInt(request.params.amount, 10);
- // Create an object to represent the new asset
- var astroDollar = new StellarSdk.Asset(assetName, issuingKeys.publicKey());
- // First, the receiving account must trust the asset
- server.loadAccount(receivingKeys.publicKey())
- .then(function (receiver) {
- var transaction = new StellarSdk.TransactionBuilder(receiver)
- // The `changeTrust` operation creates (or alters) a trustline
- // The `limit` parameter below is optional
- .addOperation(StellarSdk.Operation.changeTrust({
- asset: astroDollar,
- limit: (amuntToBuy * 10).toString()
- }))
- .build();
- transaction.sign(receivingKeys);
- return server.submitTransaction(transaction);
- })
- // Second, the issuing account actually sends a payment using the asset
- .then(function () {
- return server.loadAccount(issuingKeys.publicKey())
- })
- .then(function (issuer) {
- var transaction = new StellarSdk.TransactionBuilder(issuer)
- .addOperation(StellarSdk.Operation.payment({
- destination: receivingKeys.publicKey(),
- asset: astroDollar,
- amount: amuntToBuy.toString()
- }))
- .build();
- transaction.sign(issuingKeys);
- return server.submitTransaction(transaction);
- }).then(result => {
- response.send('cenas!' + JSON.stringify(result));
- })
- .catch(function (error) {
- console.error('Error!', error);
- console.error('Error!', JSON.stringify(error.response.data));
- });
- });
- app.get('/mstest', (request, response) => {
- var bobPair = StellarSdk.Keypair.fromSecret("SDGEY7OJTCPSGGSZ77UBLDTZOILZHG5RRBMPJXWXB32EB6PBSLWENFQT");
- var alicePair = StellarSdk.Keypair.fromSecret("SCEN3AKZBOB5SYYYPAIQJSHYFZUIZ3T6AKPWRTXSQRGDGQ5DUA6BFMRW");
- var fromPair = StellarSdk.Keypair.fromSecret("SAA7GJMFULFLRN3JKQGFLFUA4LWUY6WXXS4ATRRPAP54B5L2DFBPMRDW");
- var toPair = StellarSdk.Keypair.fromSecret("SB4TWTDKEW2KTHSWHG4YWU5SZUNKOIL3FLMK5W7OWOA6Q2IA2VJM5W7P");
- server.loadAccount(fromPair.publicKey())
- .then(function (account) {
- var transaction = new StellarSdk.TransactionBuilder(account)
- .addOperation(StellarSdk.Operation.payment({
- destination: toPair.publicKey(),
- asset: StellarSdk.Asset.native(),
- amount: '5',
- }))
- .build();
- transaction.sign(bobPair);
- transaction.sign(alicePair);
- server.submitTransaction(transaction)
- .then(() => {
- response.send('5 lumens sent to ' + toPair.publicKey())
- })
- .catch((err) => {
- console.log(err.response.data.extras.result_codes);
- response.send('An error ocurred!')
- });
- }).then(result => {
- response.send('cenas!' + result.response.data.extras.result_codes);
- })
- .catch(function (error) {
- console.error('Error!', error);
- });
- });
- app.get('/mssetup', (request, response) => {
- var bobPair = StellarSdk.Keypair.fromSecret("SDGEY7OJTCPSGGSZ77UBLDTZOILZHG5RRBMPJXWXB32EB6PBSLWENFQT");
- var alicePair = StellarSdk.Keypair.fromSecret("SCEN3AKZBOB5SYYYPAIQJSHYFZUIZ3T6AKPWRTXSQRGDGQ5DUA6BFMRW");
- //
- var fromPair = StellarSdk.Keypair.fromSecret("SAA7GJMFULFLRN3JKQGFLFUA4LWUY6WXXS4ATRRPAP54B5L2DFBPMRDW");
- var toPair = StellarSdk.Keypair.fromSecret("SB4TWTDKEW2KTHSWHG4YWU5SZUNKOIL3FLMK5W7OWOA6Q2IA2VJM5W7P");
- server.loadAccount(fromPair.publicKey()).then((account) => {
- var transaction = new StellarSdk.TransactionBuilder(account)
- .addOperation(StellarSdk.Operation.setOptions({
- signer: {
- ed25519PublicKey: bobPair.publicKey(),
- weight: 1
- },
- signer: {
- ed25519PublicKey: alicePair.publicKey(),
- weight: 1
- },
- masterWeight: 0,
- lowThreshold: 1,
- medThreshold: 1,
- highThreshold: 1
- }))
- .build();
- transaction.sign(fromPair);
- //transaction.sign(bobPair);
- //transaction.sign(alicePair);
- server.submitTransaction(transaction)
- .then(() => {
- response.send('worked!')
- })
- .catch((err) => {
- console.log(err.response.data.extras.result_codes);
- response.send('An error ocurred!')
- });
- }).catch((err) => {
- console.log(err.response.data.extras.result_codes);
- response.send('An error ocurred2!')
- });
- });
- app.get('/:account', (request, response) => {
- var pair = StellarSdk.Keypair.fromSecret(request.params.account);
- server.loadAccount(pair.publicKey()).then((account) => {
- var result = { account: pair.publicKey(), balance: [] };
- account.balances.forEach((balance) => {
- result.balance.push({ type: balance.asset_type, code: balance.asset_code, balance: balance.balance });
- });
- response.send(result);
- });
- });
- app.listen(3000, () => console.log('Example app listening on port 3000!'))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement