0

This code successfully creates a new account, and funds it with 5 XLM, but it does not add trust lines to the account. Is it acceptable to chain a changeTrust operation within this transaction? Or do I need to construct a separate change trust operation, signed by the new account?

This also does not write to firebase. Any idea why?

const functions = require('firebase-functions');
const admin = require('firebase-admin');
admin.initializeApp();

const StellarSdk = require('stellar-sdk');
StellarSdk.Network.useTestNetwork();
var server = new StellarSdk.Server('https://horizon-testnet.stellar.org');

var publicKey = 'xxx';
var secretKey = 'xxx';
var sourceKeyPair = StellarSdk.Keypair.fromSecret(secretKey);
var issuerPublicKey = 'xxx';

exports.createAccount = functions.https.onCall((data, context) => {

    var newPublicKey = data.publicKey;

    server.loadAccount(publicKey)
    .then(function(source) {

      var transaction = new StellarSdk.TransactionBuilder(source)

      .addOperation(StellarSdk.Operation.createAccount({
        destination: newPublicKey,
        startingBalance: '5',
      }))
      .addOperation(StellarSdk.Operation.changeTrust({ 
        asset: new StellarSdk.Asset('USD', issuerPublicKey),
      }))
      .addOperation(StellarSdk.Operation.changeTrust({
        asset: new StellarSdk.Asset('GOLD', issuerPublicKey),
      }))
      .setTimeout(StellarSdk.TimeoutInfinite)
      .build();

    transaction.sign(sourceKeyPair);

    server.submitTransaction(transaction)
        .then(function(transactionResult) {
            console.log(JSON.stringify(transactionResult, null, 2));
            admin.firestore().collection('users').doc(newPublicKey).set({
                publicKey: newPublicKey,
            })
        })
        .catch(function(err) {
            console.log('An error has occured:');
            console.log(err);
          });
    })
    .catch(function(err) {
        console.error(err);
    })

})
1

You have to specify the affected new account, otherwise all operations within a transaction refer to the account that creates the transaction (sourceKeyPair in your case).

.addOperation(StellarSdk.Operation.changeTrust({
              asset: asset,
              source: newPublicKey
          }))
  • 1
    ... and apply one more signature on the transaction with corresponding secret key. – Orbit Lens Jan 31 at 23:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.