Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
// Now check if the destination account is empty or not
let unfundedDestination = false;
try {
yield request.get(destinationUrl);
} catch (e) {
if (e.status === 404) {
// If the destination account does not yet exist, horizon responds with 404
unfundedDestination = true;
}
}
if (!accountData.sequence || !accountData.balances) {
throw new Error('Horizon server error - unable to retrieve sequence ID or account balance');
}
const account = new stellar.Account(params.rootAddress, accountData.sequence);
// Stellar supports multiple assets on chain, we're only interested in the balances entry whose type is "native" (XLM)
const nativeBalanceInfo = accountData.balances.find(assetBalance => assetBalance['asset_type'] === 'native');
if (!nativeBalanceInfo) {
throw new Error('Provided wallet has a balance of 0 XLM, recovery aborted');
}
const walletBalance = Number(self.bigUnitsToBaseUnits(nativeBalanceInfo.balance));
const minimumReserve: number = yield self.getMinimumReserve();
const baseTxFee: number = yield self.getBaseTransactionFee();
const recoveryAmount = walletBalance - minimumReserve - baseTxFee;
const formattedRecoveryAmount = self.baseUnitsToBigUnits(recoveryAmount).toString();
const txBuilder = new stellar.TransactionBuilder(account);
const operation = unfundedDestination ?
const login = (privateKey) => {
const keypair = privateKey
? sdk.Keypair.fromSecret(privateKey)
: sdk.Keypair.random()
localStorage.setItem(constants.privateKeyNames.xlm, keypair.secret())
const account = new sdk.Account(keypair.publicKey(), '1')
const address = keypair.publicKey()
const data = {
account,
keypair,
address,
}
console.info('Logged in with Stellar', data)
reducers.user.setAuthData({ name: 'xlmData', data })
}
export const sendTransaction = (authData, { operations, operation, memo }) => {
const keypair = KeypairInstance(authData.keypair);
const sourceAccount = authData.sourceAccount;
const sourceAddress = keypair.publicKey();
const sequenceNumber = sourceAccount.sequence;
const transAccount = new Account(sourceAddress, sequenceNumber);
const transactionBuilder = new TransactionBuilder(transAccount);
addOperations(transactionBuilder, { operations, operation });
addMemo(transactionBuilder, memo);
const transaction = transactionBuilder.build();
transaction.sign(keypair);
return Stellar.getServerInstance().submitTransaction(transaction);
};
export async function create(network: KinNetwork, keys: Keypair) {
const operations = Operations.for(network.server, keys, network.asset);
const accountResponse = await operations.loadAccount(keys.publicKey());
const account = new Account(accountResponse.accountId(), accountResponse.sequenceNumber());
const nativeBalance = accountResponse.balances.find(isNativeBalance);
const kinBalance = getKinBalance(accountResponse, network.asset);
if (!nativeBalance) {
throw new Error("account contains no balance");
}
return Wallet.create(operations, network, keys, account, nativeBalance, kinBalance);
}
export const changeTrust = ({ asset, limit, keypair, sourceAccount }) => {
const sequenceNumber = sourceAccount.sequence;
const sourceAddress = keypair.accountId();
const transAccount = new Stellar.Account(sourceAddress, sequenceNumber);
const trustLimit = isString(limit) ? limit : undefined;
const transaction = new Stellar.TransactionBuilder(transAccount)
.addOperation(Stellar.Operation.changeTrust({
asset,
limit: trustLimit,
}))
.build();
transaction.sign(keypair);
return getServerInstance()
.submitTransaction(transaction);
};