-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathbitcoin.js
191 lines (155 loc) · 5.6 KB
/
bitcoin.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
/*
* 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 Bitcoin() {
this.settings;
this.debug;
this.ajaxRequests = new Array();
this.log;
this.prepareAuth = function(user, password) {
return "Basic " + window.btoa(user + ":" + password);
}
this.RPC = function(method, params, callback, context) {
var request;
var auth = this.settings.auth;
if(params != null) {
request = {method: method, params: params};
} else {
request = {method: method};
}
var me = this;
var req = jQuery.ajax({url: this.settings.url, dataType: 'json', type: 'POST',
contentType: 'application/json',
data: JSON.stringify(request),
timeout: 15000,
beforeSend: function(req){
req.setRequestHeader("Authorization", auth);
},
success:
function(data, textStatus, req) {
jQuery.proxy( function(req) {
this.ajaxRequests = jQuery.grep(this.ajaxRequests, function (n, i) { return n.request != req; });
this.debugAJAX();
}, me)(req);
callback(data.result, data.error, context);
},
error:
function(req, textStatus, error) {
jQuery.proxy( function(req) {
this.ajaxRequests = jQuery.grep(this.ajaxRequests, function (n, i) { return n.request != req; });
this.debugAJAX();
}, me)(req);
var data;
try {
if (!req.responseText)
throw "no responseText";
data = jQuery.parseJSON(req.responseText);
} catch (err) {
data = {result: null, error: {code: req.status}};
if (data.error.code === 0 && !req.status)
data.error.message = "RPC not found";
else {
if (req.statusText)
data.error.message = req.statusText;
else
data.error.message = data.error.code.toString();
}
}
callback(data.result, data.error, context);
}
});
this.ajaxRequests.push({request: req, data: request});
this.debugAJAX();
}
this.requestsPending = function() {
return this.ajaxRequests.length;
}
this.abortAll = function() {
for (var key in this.ajaxRequests) {
/* Try to abort, request might disappear while looping */
try {
var m = this.ajaxRequests[key].data.method;
/* don't abort requests that manipulate bitcoins */
if (m == "sendfrom" || m == "move")
continue;
this.ajaxRequests[key].request.abort();
} catch (err) {
}
}
}
this.debugAJAX = function() {
if (!this.debug) return;
if (!this.log) {
this.log = jQuery('<ul></ul>');
this.log.css('position', 'absolute').css('top', 0).css('left', 0).css('color', 'black');
jQuery('body').append(this.log);
}
this.log.children().remove();
for (var key in this.ajaxRequests) {
var item = jQuery('<li/>');
item.html(key + " " + JSON.stringify(this.ajaxRequests[key].data));
this.log.append(item);
}
}
this.listAccounts = function(callback, context) {
this.RPC("listaccounts", null, callback, context);
}
this.listTransactions = function(callback, count, context) {
this.RPC("listtransactions", [this.settings.account, count], callback, context);
}
this.validateAddress = function(callback, address, context) {
this.RPC("validateaddress", [address], callback, context);
}
this.sendBTCToAddress = function(callback, context, callback_context) {
this.RPC("sendtoaddress", [context.address, context.amount, context.comment, context.payee], callback, callback_context);
}
this.sendBTCToAddressWithPassphrase = function(callback, context, callback_context) {
var unlockWalletCallback = function(result, error, context) {
var callback = context.originalCallback;
var callback_context = context.originalCallbackContext;
context.originalContext.RPC("sendtoaddress", [context.address, context.amount, context.comment, context.payee], callback, callback_context);
}
callback_context.originalCallback = callback;
callback_context.originalCallbackContext = callback_context;
callback_context.originalContext = this;
this.RPC("walletpassphrase", [context.passphrase, context.passphrasetimeout], unlockWalletCallback, callback_context);
}
this.sendBTC = function(callback, context, callback_context) {
this.RPC("sendfrom", [this.settings.account, context.address, context.amount, 1 /*minconf*/, context.comment, context.payee], callback, callback_context);
}
this.createAccount = function(callback, account, context) {
this.RPC("getaccountaddress", [account], callback, context);
}
this.getAddress = function(callback, context) {
this.RPC("getaccountaddress", [this.settings.account], callback, context);
}
this.getAddressByAccount = function(callback, account, context) {
this.RPC("getaddressesbyaccount", [account], callback, context);
}
this.getBalance = function(callback, confirmations, context) {
this.RPC("getbalance", [this.settings.account, confirmations], callback, context);
}
this.getInfo = function(callback, context) {
this.RPC("getinfo", null, callback, context);
}
this.selectAccount = function(account) {
if (account != undefined)
this.settings.account = account;
else
this.settings.account = "";
}
this.setup = function(settings) {
this.settings = {url: null, auth: null, account: null};
for (var k in this.settings) {
if (settings[k])
this.settings[k] = settings[k];
}
if (this.settings.url == "")
this.settings.url = window.location.href;
if (!this.settings.auth)
this.settings.auth = this.prepareAuth(settings.user, settings.password);
this.selectAccount(settings.account);
}
}