<commit_before>/* global _, angular, console, Decimal, StellarSdk */

angular.module('app')
.factory('Wallet', function ($http, $q, $rootScope, $timeout, $window, History, Horizon, Keychain, Storage) {
	'use strict';

	//------------------------------------------------------------------------------------------------------------------
	//	Account
	//------------------------------------------------------------------------------------------------------------------

	function _sortAssetCodes(a, b) {
		return (a.asset_code > b.asset_code) - (a.asset_code < b.asset_code);
	}

	function parseBalances(res) {
		var native = res.balances.filter(function (e) {
			return e.asset_type === 'native';
		});

		var credit_alphanum4 = res.balances.filter(function (e) {
			return e.asset_type === 'credit_alphanum4';
		});

		var credit_alphanum12 = res.balances.filter(function (e) {
			return e.asset_type === 'credit_alphanum12';
		});

		credit_alphanum4.sort(_sortAssetCodes);
		credit_alphanum12.sort(_sortAssetCodes);

		native[0].asset_code = 'XLM';
		return native.concat(credit_alphanum4, credit_alphanum12);
	}

	function Account(params) {

		this.getAccountInfo = function () {

			var self = this;

			return self.horizon().accounts()
			.accountId(self.id).call()
			.catch(function (err) {
				$timeout(self.refresh.bind(self), 60000);
				return $q.reject(err);
			})
			.then(function (res) {
				self.balances		= parseBalances(res);
				self.flags			= res.flags;
				self.sequence		= res.sequence;
				self.signers		= res.signers;
				self.subentryCount	= res.subentry_count;
				self.thresholds		= res.thresholds;

				Storage.setItem('account.' + self.alias, self);
			});
		};

		$window._.extend(this, params);
		this.refresh();
	}

	Account.prototype.horizon = function () {
		return Horizon.getServer(this.network);
	};

	Account.prototype.getNativeBalance = function () {
		return this.balances[0].balance;
	};

	Account.prototype.getReserve = function () {
		return 10 * (2 + this.subentryCount);
	};

	Account.prototype.refresh = function () {

		console.log('refreshing ' + this.alias);

		if (this.closeStream) {
			this.closeStream();
		}

		var self = this;
		return this.getAccountInfo()
		.then(
			function () {
				return History.getTransactions(self, 20)
				.then(function () {
					History.subscribe(self);
				});
			},
			function (err) {
				console.log(err);
			}
		);
	};

	//------------------------------------------------------------------------------------------------------------------
	//	Wallet
	//------------------------------------------------------------------------------------------------------------------

	var accounts = {};
	var currentAccount;

	var Wallet = {
		accounts: accounts,
		get current () {
			return currentAccount;
		},
		set current (account) {
			currentAccount = account;
			Storage.setItem('currentAccount', account.alias);
		}
	};

	Wallet.createEmptyAccount = function (name, network) {

		if (!network) {
			network = Horizon.livenet;
		}

		var keys = StellarSdk.Keypair.random();
		var accountId = keys.accountId();
		var seed = keys.seed();

		Keychain.addKey(accountId, seed);

		var opts = {
			id:			accountId,
			network:	network,
			alias:		name,
			balances: [{
				asset_type: 'native',
				asset_code: 'XLM',
				balance: '0'
			}]
		};

		var self = new Account(opts);
		accounts[self.id] = self;
		Storage.setItem('account.' + self.alias, self);

		accountList.push(self.alias);
		Storage.setItem('accounts', accountList);

		Wallet.current = self;
		return self;
	};

	Wallet.importAccount = function (accountId, seed, name, network) {

		if (!network) {
			network = Horizon.livenet;
		}

		Keychain.addKey(accountId, seed);

		var opts = {
			id:			accountId,
			network:	network,
			alias:		name,
			balances: [{
				asset_type: 'native',
				asset_code: 'XLM',
				balance: '0'
			}]
		};

		var self = new Account(opts);
		accounts[self.id] = self;
		Storage.setItem('account.' + self.alias, self);

		accountList.push(self.alias);
		Storage.setItem('accounts', accountList);

		Wallet.current = self;
		return self;
	};

	Wallet.renameAccount = function (account, newName) {

		var oldName = account.alias;
		if (oldName === newName) {
			return;
		}

		History.effects[newName] = History.effects[oldName];
		account.alias = newName;
		delete History.effects[oldName];

		Storage.setItem('account.' + newName, account);
		Storage.setItem('history.' + newName, History.effects[newName]);

		var index = accountList.indexOf(oldName);
		accountList[index] = newName;
		Storage.setItem('accounts', accountList);

/*
		if (currentAccount.alias === oldName) {
			currentAccount = account;
		}
*/
		Storage.setItem('currentAccount', newName);
		Storage.removeItem('account.' + oldName);
		Storage.removeItem('history.' + oldName);
	};<commit_msg>Rewrite createEmptyAccount in terms of importAccount<commit_after>/* global _, angular, console, Decimal, StellarSdk */

angular.module('app')
.factory('Wallet', function ($http, $q, $rootScope, $timeout, $window, History, Horizon, Keychain, Storage) {
	'use strict';

	//------------------------------------------------------------------------------------------------------------------
	//	Account
	//------------------------------------------------------------------------------------------------------------------

	function _sortAssetCodes(a, b) {
		return (a.asset_code > b.asset_code) - (a.asset_code < b.asset_code);
	}

	function parseBalances(res) {
		var native = res.balances.filter(function (e) {
			return e.asset_type === 'native';
		});

		var credit_alphanum4 = res.balances.filter(function (e) {
			return e.asset_type === 'credit_alphanum4';
		});

		var credit_alphanum12 = res.balances.filter(function (e) {
			return e.asset_type === 'credit_alphanum12';
		});

		credit_alphanum4.sort(_sortAssetCodes);
		credit_alphanum12.sort(_sortAssetCodes);

		native[0].asset_code = 'XLM';
		return native.concat(credit_alphanum4, credit_alphanum12);
	}

	function Account(params) {

		this.getAccountInfo = function () {

			var self = this;

			return self.horizon().accounts()
			.accountId(self.id).call()
			.catch(function (err) {
				$timeout(self.refresh.bind(self), 60000);
				return $q.reject(err);
			})
			.then(function (res) {
				self.balances		= parseBalances(res);
				self.flags			= res.flags;
				self.sequence		= res.sequence;
				self.signers		= res.signers;
				self.subentryCount	= res.subentry_count;
				self.thresholds		= res.thresholds;

				Storage.setItem('account.' + self.alias, self);
			});
		};

		$window._.extend(this, params);
		this.refresh();
	}

	Account.prototype.horizon = function () {
		return Horizon.getServer(this.network);
	};

	Account.prototype.getNativeBalance = function () {
		return this.balances[0].balance;
	};

	Account.prototype.getReserve = function () {
		return 10 * (2 + this.subentryCount);
	};

	Account.prototype.refresh = function () {

		console.log('refreshing ' + this.alias);

		if (this.closeStream) {
			this.closeStream();
		}

		var self = this;
		return this.getAccountInfo()
		.then(
			function () {
				return History.getTransactions(self, 20)
				.then(function () {
					History.subscribe(self);
				});
			},
			function (err) {
				console.log(err);
			}
		);
	};

	//------------------------------------------------------------------------------------------------------------------
	//	Wallet
	//------------------------------------------------------------------------------------------------------------------

	var accounts = {};
	var currentAccount;

	var Wallet = {
		accounts: accounts,
		get current () {
			return currentAccount;
		},
		set current (account) {
			currentAccount = account;
			Storage.setItem('currentAccount', account.alias);
		}
	};

	Wallet.createEmptyAccount = function (name, network) {

		var keys = StellarSdk.Keypair.random();
		var accountId = keys.accountId();
		var seed = keys.seed();

		return Wallet.importAccount(accountId, seed, name, network);
	};

	Wallet.importAccount = function (accountId, seed, name, network) {

		if (!network) {
			network = Horizon.livenet;
		}

		Keychain.addKey(accountId, seed);

		var opts = {
			id:			accountId,
			network:	network,
			alias:		name,
			balances: [{
				asset_type: 'native',
				asset_code: 'XLM',
				balance: '0'
			}]
		};

		var self = new Account(opts);
		accounts[self.id] = self;
		Storage.setItem('account.' + self.alias, self);

		accountList.push(self.alias);
		Storage.setItem('accounts', accountList);

		Wallet.current = self;
		return self;
	};

	Wallet.renameAccount = function (account, newName) {

		var oldName = account.alias;
		if (oldName === newName) {
			return;
		}

		History.effects[newName] = History.effects[oldName];
		account.alias = newName;
		delete History.effects[oldName];

		Storage.setItem('account.' + newName, account);
		Storage.setItem('history.' + newName, History.effects[newName]);

		var index = accountList.indexOf(oldName);
		accountList[index] = newName;
		Storage.setItem('accounts', accountList);

/*
		if (currentAccount.alias === oldName) {
			currentAccount = account;
		}
*/
		Storage.setItem('currentAccount', newName);
		Storage.removeItem('account.' + oldName);
		Storage.removeItem('history.' + oldName);
	};
{'fraction_non_alphanumeric': 0.23521172287469022, 'fraction_numerical': 0.005495097511044068, 'mean_word_length': 2.3740458015267176, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '641846', 'n_tokens_mistral': 3362, 'n_tokens_neox': 2873, 'n_words': 543}