I am trying to create a trustline between issuer's account and receiver's account to send Custom Assets(token)
I am following this link
var StellarSdk = require('stellar-sdk');
StellarSdk.Network.useTestNetwork();
var horizon = new StellarSdk.Server('https://horizon-testnet.stellar.org');
async function main() {
let source = StellarSdk.Keypair.fromSecret('distributor's secret key');
let dest = StellarSdk.Keypair.fromSecret('receiver's secret key');
// Keypair for the destination account can be generated using StellarSdk.Keypair.random().
let assetCode = 'COOL';
let assetIssuerAddress = 'GANQDVASPYIVJF7YJNFZJ6DPXEWKI57NRYVSC7WYM6ZAL5J7FVPXS3NU';
// 2. Load current source account state from Horizon server
let sourceAccount = await horizon.loadAccount(source.publicKey());
// 3. Create a transaction builder
let builder = new StellarSdk.TransactionBuilder(sourceAccount,opts={fee:200});
// 4. Add CHANGE_TRUST operation to establish trustline
builder.addOperation(StellarSdk.Operation.changeTrust({
asset: new StellarSdk.Asset(assetCode, assetIssuerAddress),
amount: '100',
source: dest.publicKey()
}))
// Note that source parameter contains a public key of our destination account
because we perform this operation on behalf of the destination account.
// 5. Add PAYMENT operation to transfer your custom asset
builder.addOperation(StellarSdk.Operation.payment({
destination: dest.publicKey(),
asset: new StellarSdk.Asset(assetCode, assetIssuerAddress),
amount: '10'
}))
// 6. Build and sign transaction with both source and destination keypairs
let tx = builder.setTimeout(180).build()
tx.sign(source)
tx.sign(dest)
// 7. Submit transaction to network
let txResult = await horizon.submitTransaction(tx)
console.log(txResult);
}
main();
Above code works perfectly fine but I am wondering why do we need receiver's and distributor's secretkey/privatekey to create trustline b/w them.
And what can be the role of allowTrust
operation in this transaction.
P.S : Receiver's balance is more than minimum required to be on the ledger