<commit_before>'use strict';

// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Address Creation  ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// Address creation
eID('create-address').onclick = function() {
	tooltip('Creating...', this);
	var call = {
		url: '/wallet/address',
		type: 'GET',
	};
	IPC.sendToHost('api-call', call, 'new-address');
};

// Filter address list by search string
eID('search-bar').onkeyup = function() {
	tooltip('Searching...', this);
	var searchstr = eID('search-bar').value;

	NodeList.prototype.forEach = Array.prototype.forEach
	var entries = eID('address-list').childNodes;
	entries.forEach( function(entry) {
		if (entry.querySelector('.address').innerHTML.indexOf(searchstr)) {
			hide(entry);
		} else {
			show(entry);
		}
	});
};

// Adds an address to the address list
function appendAddress(address) {
	if (eID(address)) {
		return;
	}
	var entry = eID('addressbp').cloneNode(true);
	entry.id = address;
	entry.querySelector('.address').innerHTML = address;
	show(entry);
	eID('address-list').appendChild(entry);
}

// Add the new address
addResultListener('new-address', function(result) {
	notify('New address created', 'created');
	appendAddress(result);
});

// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Transactions ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// Define send call
function sendCoin(amount, address) {
	var transaction = {
		amount: amount.toString(),
		destination: address,
	};
	var call = {
		url: '/wallet/siacoins',
		type: 'POST',
		args: transaction,
	};
	IPC.sendToHost('api-call', call, 'coin-sent');

	// Reflect it asap
	setTimeout(update, 100);
}

// Transaction has to be legitimate
// TODO: verify address
function verifyTransaction(caller, callback) {
	var amount = eID('transaction-amount').value;
	var e = eID('send-unit');
	var unit = e.options[e.selectedIndex].value;
	var address = eID('transaction-address').value;

	// Verify number
	if (!isNumber(amount)) {
		tooltip('Enter numeric amount of Siacoin to send!', caller);
		return;
	} 
	// Verify balance
	if (wallet.Balance < amount) {
		tooltip('Balance too low!', caller);
		return;
	} 
	// Verify address
	if (!isAddress(address)) {
		tooltip('Enter correct address to send to!', caller);
		return;
	}

	var total = new BigNumber(amount).times(unit);
	callback(total, address);
}

// Button to send coin
eID('send-money').onclick = function() {
	verifyTransaction(this, function() {
		tooltip('Are you sure?', eID('confirm'));
		eID('confirm').classList.remove('transparent');
	});
};

// Button to confirm transaction
eID('confirm').onclick = function() {
	// If the button's transparent, don't do anything
	if (eID('confirm').classList.contains('transparent')) {
		return;
	}
	verifyTransaction(this, function(amount, address) {
		tooltip('Sending...', eID('confirm'));
		sendCoin(amount, address);
	});
	eID('confirm').classList.add('transparent');
};

// Transaction was sent
addResultListener('coin-sent', function(result) {
	notify('Transaction sent to network!', 'sent');
	eID('transaction-amount').value = '';
});

// Button to load all wallet transactions
eID('view-all-transactions').onclick = function() {
	tooltip('Loading all transactions', this);
	IPC.sendToHost('api-call', {
		url: '/wallet/transactions',
		args: {
			startheight: 0,
			endheight: 1000000,
		},		
		type: 'GET',
	}, 'update-history');
}

// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Capsule ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// Lock or unlock the wallet
eID('lock-pod').onclick = function() {
	var state = eID('lock-status').innerHTML;
	if (!wallet.unlocked && state === 'Unencrypted') {
		encrypt();
	} else if (wallet.unlocked && state === 'Lock Wallet') {
		lock();
	} else if (!wallet.unlocked && state === 'Unlock Wallet'){<commit_msg>enable wallet unlocking<commit_after>'use strict';

// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Address Creation  ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// Address creation
eID('create-address').onclick = function() {
	tooltip('Creating...', this);
	var call = {
		url: '/wallet/address',
		type: 'GET',
	};
	IPC.sendToHost('api-call', call, 'new-address');
};

// Filter address list by search string
eID('search-bar').onkeyup = function() {
	tooltip('Searching...', this);
	var searchstr = eID('search-bar').value;

	NodeList.prototype.forEach = Array.prototype.forEach
	var entries = eID('address-list').childNodes;
	entries.forEach( function(entry) {
		if (entry.querySelector('.address').innerHTML.indexOf(searchstr)) {
			hide(entry);
		} else {
			show(entry);
		}
	});
};

// Adds an address to the address list
function appendAddress(address) {
	if (eID(address)) {
		return;
	}
	var entry = eID('addressbp').cloneNode(true);
	entry.id = address;
	entry.querySelector('.address').innerHTML = address;
	show(entry);
	eID('address-list').appendChild(entry);
}

// Add the new address
addResultListener('new-address', function(result) {
	notify('New address created', 'created');
	appendAddress(result);
});

// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Transactions ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// Define send call
function sendCoin(amount, address) {
	var transaction = {
		amount: amount.toString(),
		destination: address,
	};
	var call = {
		url: '/wallet/siacoins',
		type: 'POST',
		args: transaction,
	};
	IPC.sendToHost('api-call', call, 'coin-sent');

	// Reflect it asap
	setTimeout(update, 100);
}

// Transaction has to be legitimate
// TODO: verify address
function verifyTransaction(caller, callback) {
	var amount = eID('transaction-amount').value;
	var e = eID('send-unit');
	var unit = e.options[e.selectedIndex].value;
	var address = eID('transaction-address').value;

	// Verify number
	if (!isNumber(amount)) {
		tooltip('Enter numeric amount of Siacoin to send!', caller);
		return;
	} 
	// Verify balance
	if (wallet.Balance < amount) {
		tooltip('Balance too low!', caller);
		return;
	} 
	// Verify address
	if (!isAddress(address)) {
		tooltip('Enter correct address to send to!', caller);
		return;
	}

	var total = new BigNumber(amount).times(unit);
	callback(total, address);
}

// Button to send coin
eID('send-money').onclick = function() {
	verifyTransaction(this, function() {
		tooltip('Are you sure?', eID('confirm'));
		eID('confirm').classList.remove('transparent');
	});
};

// Button to confirm transaction
eID('confirm').onclick = function() {
	// If the button's transparent, don't do anything
	if (eID('confirm').classList.contains('transparent')) {
		return;
	}
	verifyTransaction(this, function(amount, address) {
		tooltip('Sending...', eID('confirm'));
		sendCoin(amount, address);
	});
	eID('confirm').classList.add('transparent');
};

// Transaction was sent
addResultListener('coin-sent', function(result) {
	notify('Transaction sent to network!', 'sent');
	eID('transaction-amount').value = '';
});

// Button to load all wallet transactions
eID('view-all-transactions').onclick = function() {
	tooltip('Loading all transactions', this);
	IPC.sendToHost('api-call', {
		url: '/wallet/transactions',
		args: {
			startheight: 0,
			endheight: 1000000,
		},		
		type: 'GET',
	}, 'update-history');
}

// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Capsule ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// Lock or unlock the wallet
eID('lock-pod').onclick = function() {
	var state = eID('lock-status').innerHTML;
	if (!wallet.unlocked && state === 'Create Wallet') {
		encrypt();
	} else if (wallet.unlocked && state === 'Lock Wallet') {
		lock();
	} else if (!wallet.unlocked && state === 'Unlock Wallet'){
{'fraction_non_alphanumeric': 0.21784847667834997, 'fraction_numerical': 0.0029657589646805067, 'mean_word_length': 4.715716486902927, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 3, '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': '2208432', 'n_tokens_mistral': 2426, 'n_tokens_neox': 2319, 'n_words': 540}