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);
})
})