Skip to content

Commit

Permalink
Use a single change address for reliability
Browse files Browse the repository at this point in the history
  • Loading branch information
kyledrake committed Mar 16, 2014
2 parents 88a37a9 + 6d2ebff commit af2674f
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 35 deletions.
18 changes: 6 additions & 12 deletions lib/coinpunk/server/db/redis.js
Original file line number Diff line number Diff line change
Expand Up @@ -90,21 +90,15 @@ DB.prototype = {

this.redis.hgetall(serverKey, function(err, res) {
if(err)
callback('database error: '+err);
return callback('database error: '+err);

if(res && res.payloadHash != undefined && res.payloadHash != 'undefined' && payload.originalPayloadHash != undefined && payload.originalPayloadHash != res.payloadHash) {
self.getWallet(serverKey, function(err, wallet) {
return callback('outOfSync', {wallet: wallet});
});
} else {
if(payload.newPayloadHash)
payload.payloadHash = payload.newPayloadHash;
if(payload.newPayloadHash)
payload.payloadHash = payload.newPayloadHash;

delete payload.originalPayloadHash;
delete payload.newPayloadHash;
delete payload.originalPayloadHash;
delete payload.newPayloadHash;

self.redis.hmset(serverKey, payload, callback);
}
self.redis.hmset(serverKey, payload, callback);
});

},
Expand Down
4 changes: 2 additions & 2 deletions public/js/all.js

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions public/js/coinpunk/controllers/accounts.js
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,7 @@ coinpunk.controllers.Accounts.prototype.create = function() {

var wallet = new coinpunk.Wallet();
var address = wallet.createNewAddress('Default');
var change = wallet.createNewAddress('change', true);
var walletKey = wallet.createWalletKey(email, password);

coinpunk.wallet = wallet;
Expand Down
47 changes: 26 additions & 21 deletions public/js/coinpunk/controllers/tx.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,27 @@ coinpunk.controllers.Tx.prototype.details = function(txHash) {
coinpunk.controllers.Tx.prototype.send = function() {
var self = this;

this.getUnspent(function(resp) {
coinpunk.router.render('view', 'tx/send', {balance: coinpunk.wallet.safeUnspentBalance()}, function(id) {
self.updateExchangeRates(id, false);
$('#'+id+" [rel='tooltip']").tooltip();
});
if(coinpunk.wallet.getChangeAddress())
return renderPage();

var changeAddress = coinpunk.wallet.createNewAddress('change', true);

this.saveWallet({override: true, address: changeAddress}, function(response) {
if(response.result != 'ok') {
return coinpunk.router.route('signout');
}

renderPage();
});

function renderPage() {
self.getUnspent(function(resp) {
coinpunk.router.render('view', 'tx/send', {balance: coinpunk.wallet.safeUnspentBalance()}, function(id) {
self.updateExchangeRates(id, false);
$('#'+id+" [rel='tooltip']").tooltip();
});
});
};
};

coinpunk.controllers.Tx.prototype.sendExchangeUpdate = function() {
Expand Down Expand Up @@ -110,20 +125,15 @@ coinpunk.controllers.Tx.prototype.create = function() {
return;
}

var changeAddress = $('#changeAddress').val();

if(changeAddress == '')
changeAddress = coinpunk.wallet.createNewAddress('change', true);
var rawtx = coinpunk.wallet.createSend(amount, calculatedFee, address, coinpunk.wallet.getChangeAddress());

var rawtx = coinpunk.wallet.createSend(amount, calculatedFee, address, changeAddress);

self.saveWallet({override: true, address: changeAddress}, function(response) {
self.saveWallet({override: true}, function(response) {
if(response.result == 'error' && response.messages[0] == 'Invalid session key') {
self.displayErrors(['Fatal error: invalid session key, tx was not sent, logging out'], errorsDiv);
delete coinpunk.wallet;
coinpunk.router.route('signout');
} else if(response.result != 'ok') {
self.displayErrors(['An unknown error has occured, tx was not sent. Logging out. Please try again later.'], errorsDiv);
delete coinpunk.wallet;
coinpunk.router.route('signout');
} else {
$.post('/api/tx/send', {tx: rawtx}, function(resp) {
if(resp.error) {
Expand All @@ -132,7 +142,7 @@ coinpunk.controllers.Tx.prototype.create = function() {
return self.saveWallet({override: true}, function(walletResponse) {
if(response.result != 'ok') {
self.displayErrors(['An unknown error has occured, tx was not sent and could not revert tx info. Logging out. Please reload and try again later.'], errorsDiv);
delete coinpunk.wallet;
coinpunk.router.route('signout');
}
self.displayErrors([resp.error.message], errorsDiv);
sendButton.removeClass('disabled');
Expand Down Expand Up @@ -173,14 +183,9 @@ coinpunk.controllers.Tx.prototype.calculateFee = function() {
if(address == '' || amount == '')
return;

var changeAddress = $('#changeAddress').val();
var changeAddress = coinpunk.wallet.getChangeAddress();
var calculatedFee = $('#calculatedFee').val();

if(changeAddress == '') {
changeAddress = coinpunk.wallet.createNewAddress('change', true);
$('#changeAddress').val(changeAddress);
}

var calculatedFee = coinpunk.wallet.calculateFee(amount, address, changeAddress);
$('#calculatedFee').val(calculatedFee);
$('#fee').text(coinpunk.wallet.calculateFee(amount, address, changeAddress)+' BTC');
Expand Down
19 changes: 19 additions & 0 deletions public/js/coinpunk/models/wallet.js
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,15 @@ coinpunk.Wallet = function(walletKey, walletId) {
}
return addrs;
};

this.deleteChangeAddress = function() {
var newKeyPairs = [];
for(var i=0; i<keyPairs.length; i++) {
if(keyPairs[i].isChange != true)
newKeyPairs.push(keyPairs[i]);
}
keyPairs = newKeyPairs;
};

this.receiveAddressHashes = function() {
var addrHashes = [];
Expand All @@ -103,6 +112,16 @@ coinpunk.Wallet = function(walletKey, walletId) {
return addrHashes;
};

this.getChangeAddress = function() {
var addrHashes = [];
for(var i=0; i<keyPairs.length; i++) {
if(keyPairs[i].isChange == true)
return keyPairs[i].address;
}

return null;
};

this.changeAddressHashes = function() {
var addrHashes = [];
for(var i=0; i<keyPairs.length; i++) {
Expand Down

0 comments on commit af2674f

Please sign in to comment.