-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathsendbtc.js
205 lines (157 loc) · 5.18 KB
/
sendbtc.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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
/*
* Copyright (c) 2010 Nils Schneider
* Distributed under the MIT/X11 software license, see the accompanying
* file license.txt or http://www.opensource.org/licenses/mit-license.php.
*/
function SendBTC(box, app) {
/* assign class to self for event handlers */
var self = this;
this.sendCallback = function(result, error, context) {
if(error != null) {
this.showResult(error.message, false, context);
return;
}
var obj;
obj = setFormValue($('form#sendBTC'), "address", "");
hideValidation(obj);
obj = setFormValue($('form#sendBTC'), "amount", "");
hideValidation(obj);
this.showResult("Bitcoins sent", true, context);
app.refreshAll();
}
this.showResult = function(message, success, context) {
this.resetClass();
var div = this.div(true);
var button = $('<span class="button"></div>');
if (success) {
box.addClass('success');
button.text('Send more bitcoins');
button.click(this.reset.proxy(this));
} else {
box.addClass('error');
button.text('Go back to form');
button.click( function() {
self.fillAndShowForm(context);
});
}
div.append('<p><strong>' + message + '</strong></p>').addClass('center');
div.append(button.wrap('<p/>').addClass('center'));
}
this.fillAndShowForm = function(context) {
this.reset();
if (app.useSlide())
box.slideDown('fast');
else
box.show();
var form = box.find('form');
setFormValue(form, 'address', context.address);
setFormValue(form, 'amount', context.amount);
setFormValue(form, 'payee', context.payee);
setFormValue(form, 'comment', context.comment);
setFormValue(form, 'passphrase', context.passphrase);
context.passphrasetimeout = 15;
/* call validators */
form.find('input').change();
}
this.sendBTC = function(context) {
var rawcontext = context;
if (app.useSlide())
box.slideDown('fast');
else
box.show();
box.children('form').hide();
context.amount = Math.round(context.amount*1e8)/1e8;
var payeeString = "";
if (context.payee)
payeeString = " (" + $('<div/>').text(context.payee).html() + ")";
var confString = "Send <strong>" + context.amount.formatBTC() + "</strong> to " + context.address + payeeString + "?";
this.resetClass();
box.addClass('critical');
var div = this.div(true);
div.append($('<h4>').text('Confirm payment').addClass('center'));
if (context.comment) {
var commentP = $('<p/>').text(context.comment).addClass("center italic");;
div.append(commentP);
}
var confP = $('<p/>').html(confString).addClass("center");
div.append(confP);
var buttonSend = $('<span class="button buttonSend"/>').text('Send');
var buttonCancel = $('<span class="button buttonCancel"/>').text('Cancel / Edit');
buttonSend.click( function() { self.dispatchSend(context, rawcontext); });
buttonCancel.click( function() { self.fillAndShowForm(rawcontext); });
div.append(buttonSend).append(buttonCancel);
}
this.dispatchSend = function(context, rawcontext) {
/* FIXME: add busy/sending indicator */
var div = this.div(true);
div.append('<p class="center">Sending...</p>');
if (app.settings.labelsmode) {
if(context.passphrase) {
app.bitcoin.sendBTCToAddressWithPassphrase(this.sendCallback.proxy(this), context, rawcontext);
} else {
app.bitcoin.sendBTCToAddress(this.sendCallback.proxy(this), context, rawcontext);
}
} else {
app.bitcoin.sendBTC(this.sendCallback.proxy(this), context, rawcontext);
}
}
this.onValidateAddressField = function(result, error, field) {
showValidation(field, result.isvalid && $(field).val() == result.address);
}
this.div = function(show) {
var div = box.children('#sendBTCinfo');
div.contents().remove();
if (show)
div.show();
else
div.hide();
return div;
}
this.resetClass = function () {
box.removeClass('critical error success');
}
this.reset = function () {
this.div(false);
this.resetClass();
box.children('form').show();
box.children('form').children('input').val("").each( function() {
hideValidation(this);
});
}
this.init = function () {
box.find('input[name="address"]').change( function() {
if($(this).val() === "") {
hideValidation(this);
return;
}
var address = $(this).val();
app.bitcoin.validateAddress(self.onValidateAddressField.proxy(self), address, this);
});
box.find('input[name="amount"]').change( function() {
if($(this).val() === "") {
hideValidation(this);
return;
}
var amount = $(this).val();
if(amount > 0 && amount <= app.balance)
showValidation(this, true);
else
showValidation(this, false);
});
box.find('form').bind('reset', function() {
self.reset();
});
box.find('form').submit( function() {
var address = getFormValue(this, "address");
var amount = getFormValue(this, "amount");
var payee = getFormValue(this, "payee");
var comment = getFormValue(this, "comment");
var passphrase = getFormValue(this, "passphrase");
var passphrasetimeout = 20;
var context = {address: address, amount: amount, payee: payee, comment: comment, passphrase: passphrase, passphrasetimeout: passphrasetimeout};
self.sendBTC(context);
return false;
});
}
this.init();
}