forked from dsias/blockchain.info
-
Notifications
You must be signed in to change notification settings - Fork 0
/
claim.js
146 lines (109 loc) · 4.89 KB
/
claim.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
//Included in wallet pages when visiting a /wallet/claim link
var privateKeyToSweep;
function showClaimModal(key) {
var modal = $('#claim-modal');
modal.modal({
keyboard: true,
backdrop: "static",
show: true
});
modal.find('.balance').text('Loading...');
var from_address = key.getBitcoinAddress().toString();
BlockchainAPI.get_balance([from_address], function(data) {
if (data == 0) {
modal.find('.spent').show(200);
} else {
modal.find('.spent').hide(200);
}
modal.find('.balance').text('Amount: ' + formatBTC(data));
}, function() {
modal.find('.balance').text('Error Fetching Balance');
});
modal.find('.create').unbind().click(function() {
window.location = root + 'wallet/new#' + B58.encode(privateKeyToSweep.priv);
});
modal.find('.login').unbind().click(function() {
modal.modal('hide');
});
modal.find('.forward').unbind().click(function() {
$('#claim-manual').show(200);
$('#claim-manual-send').unbind().click(function() {
loadScript('wallet/signer', function() {
try {
var to_address = $.trim($('#claim-manual-address').val());
try {
var bitcoin_address = new Bitcoin.Address(to_address);
} catch (e) {
MyWallet.makeNotice('error', 'misc-error', 'Invalid Bitcoin Address');
return;
}
BlockchainAPI.get_balance([from_address], function(value) {
modal.modal('hide');
var obj = initNewTx();
obj.fee = obj.base_fee; //Always include a fee
obj.to_addresses.push({address: bitcoin_address, value : BigInteger.valueOf(value).subtract(obj.fee)});
obj.from_addresses = [from_address];
obj.extra_private_keys[from_address] = B58.encode(privateKeyToSweep.priv);
obj.ready_to_send_header = 'Bitcoins Ready to Claim.';
obj.start();
}, function() {
MyWallet.makeNotice('error', 'misc-error', 'Error Getting Address Balance');
});
} catch (e) {
MyWallet.makeNotice('error', 'misc-error', e);
}
});
});
$(this).hide();
});
}
$(document).ready(function() {
//Add an addresses from the "Add to My Wallet" link
//Check if we have any addresses to add
var hash = decodeURI(window.location.hash.replace("#", ""));
if (hash.indexOf('bitcoin:') == 0) {
MyWallet.makeNotice('error', 'error-addr', 'Hash appears to be a URI not a private key');
return;
}
//Hash No Longer Needed
window.location.hash = '';
//HTML5 remove hash
if ("pushState" in history)
history.pushState("", document.title, window.location.pathname + window.location.search);
if (!hash || hash.length == 0)
return;
try {
var format = MyWallet.detectPrivateKeyFormat(hash);
privateKeyToSweep = MyWallet.privateKeyStringToKey(hash, format);
} catch (e) {
console.log(e);
MyWallet.makeNotice('error', 'error-addr', 'Error Decoding Private Key. Could not claim coins.');
}
if (privateKeyToSweep) {
//If No guid available show the user the "claim modal" which includes a signup link
showClaimModal(privateKeyToSweep);
MyWallet.addEventListener(function(event) {
if (event == 'did_decrypt') {
if (privateKeyToSweep) {
loadScript('wallet/signer', function() {
var from_address = privateKeyToSweep.getBitcoinAddress().toString();
BlockchainAPI.get_balance([from_address], function(value) {
if (value == 0) {
MyWallet.makeNotice('error', 'misc-error', 'The payment has already been claimed');
return;
}
var obj = initNewTx();
obj.fee = obj.base_fee; //Always include a fee
obj.to_addresses.push({address: new Bitcoin.Address(MyWallet.getPreferredAddress()), value : BigInteger.valueOf(value).subtract(obj.fee)});
obj.from_addresses = [from_address];
obj.extra_private_keys[from_address] = B58.encode(privateKeyToSweep.priv);
obj.start();
}, function() {
MyWallet.makeNotice('error', 'misc-error', 'Error Getting Address Balance');
});
});
}
}
});
}
})