Skip to content

Commit 1fe536c

Browse files
committed
Make bitcoin payment code more robust
1 parent f86499e commit 1fe536c

File tree

5 files changed

+45
-10
lines changed

5 files changed

+45
-10
lines changed

.gitignore

+2
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,5 @@ src/static/js/bundle.js
88
wallets.txt
99

1010
config.codekit
11+
12+
config.codekit

config.codekit

+14-3
Original file line numberDiff line numberDiff line change
@@ -1031,12 +1031,12 @@
10311031
"outputStyle": 1,
10321032
"syntaxCheckerStyle": 1
10331033
},
1034-
"\/src\/static\/js\/BitcoinJS-lib.js": {
1034+
"\/src\/static\/js\/bitcoinjs-lib.min.js": {
10351035
"fileType": 64,
10361036
"ignore": 0,
10371037
"ignoreWasSetByUser": 0,
1038-
"inputAbbreviatedPath": "\/src\/static\/js\/BitcoinJS-lib.js",
1039-
"outputAbbreviatedPath": "\/src\/static\/js\/min\/BitcoinJS-lib-min.js",
1038+
"inputAbbreviatedPath": "\/src\/static\/js\/bitcoinjs-lib.min.js",
1039+
"outputAbbreviatedPath": "\/src\/static\/js\/min\/bitcoinjs-lib.min-min.js",
10401040
"outputPathIsOutsideProject": 0,
10411041
"outputPathIsSetByUser": 0,
10421042
"outputStyle": 1,
@@ -1064,6 +1064,17 @@
10641064
"outputStyle": 1,
10651065
"syntaxCheckerStyle": 1
10661066
},
1067+
"\/src\/static\/js\/cryptojs.min.js": {
1068+
"fileType": 64,
1069+
"ignore": 0,
1070+
"ignoreWasSetByUser": 0,
1071+
"inputAbbreviatedPath": "\/src\/static\/js\/cryptojs.min.js",
1072+
"outputAbbreviatedPath": "\/src\/static\/js\/min\/cryptojs.min-min.js",
1073+
"outputPathIsOutsideProject": 0,
1074+
"outputPathIsSetByUser": 0,
1075+
"outputStyle": 1,
1076+
"syntaxCheckerStyle": 1
1077+
},
10671078
"\/src\/static\/js\/cryptorates.js": {
10681079
"fileType": 64,
10691080
"ignore": 0,

src/static/js/SimpleWalletBitcoin.js

+20-7
Original file line numberDiff line numberDiff line change
@@ -364,11 +364,19 @@ var BTCWallet = (function () {
364364
if (CurrentAmount > (amount + 2005) / Math.pow(10,8)) {
365365
break;
366366
} else {
367-
CurrentAmount += (this.known_unspent[v].value / Math.pow(10,8));
368-
var tmp = this.known_unspent[v];
369-
// Convert value to amount
370-
tmp.amount = tmp.value;
371-
CutUnspent.push(tmp);
367+
if (this.known_unspent && this.known_unspent[v] && this.known_unspent[v].value){
368+
CurrentAmount += (this.known_unspent[v].value / Math.pow(10,8));
369+
var tmp = this.known_unspent[v];
370+
// Convert value to amount
371+
if (!tmp.amount){
372+
// Make sure to save amount as the value in full BTC and NOT satoshi
373+
tmp.amount = tmp.value / Math.pow(10,8);
374+
}
375+
CutUnspent.push(tmp);
376+
} else if (this.known_unspent && this.known_unspent[v] && this.known_unspent[v].amount){
377+
CurrentAmount += this.known_unspent[v].amount;
378+
CutUnspent.push(this.known_unspent[v]);
379+
}
372380
}
373381
}
374382
console.log((amount + 2005) / Math.pow(10,8));
@@ -472,6 +480,11 @@ var BTCWallet = (function () {
472480
console.log(unspents);
473481
for (var v in unspents) {
474482
// Add them regardless of if there is zero confirmations.
483+
if (!unspents[v].tx){
484+
if(unspents[v].txid){
485+
unspents[v].tx = unspents[v].txid
486+
}
487+
}
475488
if (unspents[v].tx && (unspents[v].n == 1 || unspents[v].n == 0)){
476489
tx.addInput(unspents[v].tx, unspents[v].n);
477490
_this.putSpent(unspents[v]);
@@ -528,7 +541,7 @@ var BTCWallet = (function () {
528541
if (toAddress == fromAddress) {
529542
_this.putUnspent({
530543
address: toAddress,
531-
txid: data.txid,
544+
txid: data.data,
532545
vout: 0,
533546
confirmations: -1,
534547
amount: amount / Math.pow(10, 8)
@@ -538,7 +551,7 @@ var BTCWallet = (function () {
538551
if (changeValue >= minFeePerKb)
539552
_this.putUnspent({
540553
address: fromAddress,
541-
txid: data.txid,
554+
txid: data.data,
542555
vout: 1,
543556
confirmations: -1,
544557
amount: changeValue / Math.pow(10, 8)

src/static/js/media.js

+1
Original file line numberDiff line numberDiff line change
@@ -461,6 +461,7 @@ function showPaymentOption(e) {
461461
sentFunds = false;
462462
watchForLocalWalletPayment(btc_wallet.getFirstAddress(), price, function (a) {
463463
console.log(a);
464+
return onPaymentDone(action, fileData);
464465
});
465466
} catch (e) {
466467
console.log(e);

src/static/js/wallet-bitcoin.js

+8
Original file line numberDiff line numberDiff line change
@@ -83,11 +83,13 @@ function watchForLocalWalletPayment(address, amount, done) {
8383
// Add in 100 extra satoshi just in case :)
8484
console.log(amount, BTCUSD);
8585
btc_wallet.sendCoins(btc_wallet.getFirstAddress(), btcAddress, amount/BTCUSD, function(err, data){
86+
8687
if (err){
8788
console.error(err);
8889
}
8990
console.log(data);
9091
sentFunds = true;
92+
9193
restartWalletWebSocket = false;
9294
bitcoinWalletWebsocket.close();
9395
});
@@ -121,14 +123,20 @@ var payArtifactFromLocalWallet = function(type){
121123

122124
var amount = $($('.pwyw-btc-' + type + '-price')[0]).text();
123125

126+
$('#payment-select-buttons-localwallet-' + type).text("Sending...").prop("disabled",true);
127+
124128
btc_wallet.sendCoins(btc_wallet.getFirstAddress(), btcAddress, parseFloat(amount), function(err, data){
125129
if (err){
126130
console.error(err);
131+
return;
127132
}
133+
$('#payment-select-buttons-localwallet-' + type).text("Sent!").prop("disabled",false);
128134
console.log(data);
129135
sentFunds = true;
130136
restartWalletWebSocket = false;
131137
bitcoinWalletWebsocket.close();
138+
var fileData = $('.playlist tr.active').data();
139+
onPaymentDone(type, fileData);
132140
});
133141
}
134142

0 commit comments

Comments
 (0)