Skip to content

Commit

Permalink
Merged Kyle's changes in.
Browse files Browse the repository at this point in the history
  • Loading branch information
Ryan Casey committed May 10, 2014
2 parents 6bf16b9 + a5b695c commit 7bd59ad
Show file tree
Hide file tree
Showing 9 changed files with 141 additions and 51 deletions.
1 change: 0 additions & 1 deletion docs/INSTALL-OSX.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@ Consult `brew info redis` to learn how to configure redis to start (and start au
Please check the instructions for [installing and configuring Insight API](https://github.com/bitpay/insight-api).
In order to run Insight API you need to install bitcoind and download the bitcoin blockchain as described on the documentation.


## Install and Configure Coinpunk

Go to your user's home directory (`cd ~`), clone the repository and install nodejs dependencies:
Expand Down
12 changes: 12 additions & 0 deletions lib/coinpunk/controllers/wallet.js
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,18 @@ WalletController.prototype._delete = function(req, res) {
WalletController.prototype.saveWalletAndAddresses = function(req, res) {
var that = this;

if(req.body.addresses) {
var batch = [];
for(var i=0;i<req.body.addresses.length;i++)
batch.push({method: 'importaddress', params: [req.body.addresses[i], req.body.serverKey, false], id: i});

try {
that.bitcoind.batch(batch, function(err, btcres) {});
} catch(e) {
// return res.send({result: 'error', messages: ['there has been an error, please try again later']})
}
}

if(req.body.address)
return that.saveWallet(req, res);

Expand Down
33 changes: 9 additions & 24 deletions lib/coinpunk/server/db/mongo.js
Original file line number Diff line number Diff line change
Expand Up @@ -114,30 +114,15 @@ DB.prototype = {

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

delete payload.originalPayloadHash;
delete payload.newPayloadHash;

Wallet.update({serverKey: serverKey}, payload, { upsert: true }, function(err) {
if (err) {
callback('database error: '+err);
}
else {
self.getWallet(serverKey, function (err, wallet) {
if (err) {
callback('database error: '+err);
}
return callback(undefined, {wallet: wallet});
});
}
});
}
return callback('database error: '+err);

if(payload.newPayloadHash)
payload.payloadHash = payload.newPayloadHash;

delete payload.originalPayloadHash;
delete payload.newPayloadHash;

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

Expand Down
56 changes: 56 additions & 0 deletions public/decrypt.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<!-- <meta name="viewport" content="width=device-width, initial-scale=1.0"> -->
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no">
<meta name="apple-mobile-web-app-capable" content="yes">
<link href="css/all.css" rel="stylesheet">
<link rel="apple-touch-icon" href="img/apple-touch-icon.png">
<title>Coinpunk</title>
<script>

function decrypt() {
try {
var email = document.getElementById('email').value
var password = document.getElementById('password').value
var wallet = document.getElementById('wallet').value
var output = document.getElementById('output')

var key = sjcl.codec.base64.fromBits(sjcl.misc.pbkdf2(password, email, 1000))

output.innerHTML = '<pre>'+JSON.stringify(JSON.parse(sjcl.decrypt(key, wallet)), null, "\t")+'</pre>'
} catch(e) {
output.innerHTML = 'error: '+e
}
}

</script>
</head>
<body>
<div id="header" style="margin-bottom: 50px"></div>
<div id="container" class="container" style="margin-bottom: 30px">
<h1>Decrypt Wallet</h1>

<form id="form" onsubmit="decrypt(); return false">
Email: <input id="email" type="text">
<br>
Password: <input id="password" type="password">
<br>
Encrypted Wallet:<br>
<textarea id="wallet" rows="10" cols="100"></textarea>
<br>
<input type="submit" value="Decrypt">
</div>

<div id="output">
</div>
</div>

<script src="js/lib/bitcoinjs.js"></script>
<script src="js/lib/sjcl.js"></script>
<script>
var Bitcoin = require('./lib/bitcoinjs/index.js');
</script>
</body>
</html>
4 changes: 2 additions & 2 deletions public/js/all.js

Large diffs are not rendered by default.

3 changes: 2 additions & 1 deletion public/js/coinpunk/controllers/accounts.js
Original file line number Diff line number Diff line change
Expand Up @@ -116,11 +116,12 @@ 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;

this.saveWallet({address: address, payload: {email: email}}, function(response) {
this.saveWallet({address: address, addresses: [change], payload: {email: email}}, function(response) {
if(response.result == 'ok') {
coinpunk.wallet.sessionKey = response.sessionKey;
coinpunk.router.listener();
Expand Down
62 changes: 40 additions & 22 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 @@ -109,28 +124,36 @@ coinpunk.controllers.Tx.prototype.create = function() {
sendButton.removeClass('disabled');
return;
}

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

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

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

self.saveWallet({override: true, address: changeAddress}, 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 {

var tx = coinpunk.wallet.createTx(amount, calculatedFee, address, changeAddress);
$.post('/api/tx/send', {tx: tx.raw}, function(resp) {
if (resp.error) {
self.displayErrors(resp.messages, errorsDiv);
sendButton.removeClass('disabled');
return;
}
else {
$.post('/api/tx/send', {tx: rawtx}, function(resp) {
if(resp.error) {
coinpunk.wallet.revertTx();

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);
coinpunk.router.route('signout');
}
self.displayErrors([resp.error.message], errorsDiv);
sendButton.removeClass('disabled');
})

} else {
coinpunk.wallet.createSend(amount, calculatedFee, address, tx);
coinpunk.database.setSuccessMessage("Sent "+amount+" BTC to "+address+".");

Expand Down Expand Up @@ -167,14 +190,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
2 changes: 1 addition & 1 deletion public/manifest.appcache
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
CACHE MANIFEST
# v20
# v22
index.html
config.json
js/all.js
Expand Down

0 comments on commit 7bd59ad

Please sign in to comment.