forked from javgh/hiveapp-api-mock
-
Notifications
You must be signed in to change notification settings - Fork 1
/
hiveapp-api-mock.js
200 lines (167 loc) · 5.41 KB
/
hiveapp-api-mock.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
/*
* Hive App API mock https://github.com/javgh/hiveapp-api-mock
*
* Licensed under the MIT License.
*
* v1.1.0
*/
var bitcoin = bitcoin || mockBitcoin()
function mockBitcoin() {
var _BTC_IN_SATOSHI = 100000000;
var _MBTC_IN_SATOSHI = 100000;
var _UBTC_IN_SATOSHI = 100;
var userAddress = 'poqjer23rfc234laq';
var transactions = [];
var exchangeRateListeners = [];
var bitcoinFormatToSatoshi = {
BTC: _BTC_IN_SATOSHI,
mBTC: _MBTC_IN_SATOSHI,
µBTC: _UBTC_IN_SATOSHI
}
var preferredBitcoinFormat = 'mBTC';
var preferredCurrency = 'USD';
var locale = navigator.language;
function async(fn) { setTimeout(fn, 0) }
function createCORSRequest(method, url) {
var xhr = new XMLHttpRequest();
if ("withCredentials" in xhr) {
xhr.open(method, url, true);
} else if (typeof XDomainRequest != "undefined") {
xhr = new XDomainRequest();
xhr.open(method, url);
} else {
throw new Error('CORS not supported');
}
return xhr;
}
function ajax(url, options) {
options = options || {}
options.type = options.type || "GET"
options.dataType = options.dataType || "json"
options.success = options.success || function(){}
options.failure = options.failure || function(){}
options.complete = options.complete || function(){}
var xhr = createCORSRequest(options.type, url)
xhr.responseType = options.dataType
xhr.onload = function(event) {
options.success(xhr.response, xhr.status)
};
xhr.onerror = function(event) {
options.error(xhr.response, xhr.status, xhr.statusText)
};
xhr.onloadend = function(event) {
options.complete(xhr.response, xhr.status, xhr.statusText)
};
var data = new FormData();
if(options.data) {
for(var key in options.data) {
data.append(key, options.data[key])
}
}
xhr.send(data);
}
return {
BTC_IN_SATOSHI: _BTC_IN_SATOSHI,
MBTC_IN_SATOSHI: _MBTC_IN_SATOSHI,
UBTC_IN_SATOSHI: _UBTC_IN_SATOSHI,
TX_TYPE_INCOMING: "incoming",
TX_TYPE_OUTGOING: "outgoing",
sendMoney: function(address, amount, callback){
if (!address) { throw "address argument is undefined" }
var result = prompt("Send bitcoins to " + address + ": ", amount);
if (callback) {
if (result) {
var txid = transactions.length + "";
transactions.push({
id: txid,
amount: amount,
type: bitcoin.TX_TYPE_OUTGOING,
timestamp: new Date().toISOString(),
inputAddresses: [userAddress],
outputAddresses: [address]
});
async(function() { callback(true, txid) } )
} else {
async(function() { callback(false) } )
}
}
},
getTransaction: function(id, callback){
if (!id || !callback){ throw "id and callback are required" }
var tx = transactions.filter(function(t){ return t.id == id })[0]
async(function(){ callback(tx) })
},
getSystemInfo: function(callback){
if (!callback){
throw "callback is undefined";
}
async(function(){
callback({
version: "0.9",
buildNumber: "2013120901",
decimalSeparator: (0.1).toLocaleString().substring(1, 2),
locale: locale,
preferredCurrency: "USD",
preferredBitcoinFormat: preferredBitcoinFormat
})
})
},
addExchangeRateListener: function(callback) {
if (!callback){ throw "callback is required" }
exchangeRateListeners.push(callback)
},
removeExchangeRateListener: function(callback) {
if (!callback){ throw "callback is required" }
exchangeRateListeners.splice(exchangeRateListeners.indexOf(callback), 1)
},
updateExchangeRate: function(currency) {
exchangeRateListeners.forEach(function(fn){
fn(currency, Number((Math.random() * 1000).toFixed(2)))
})
},
getUserInfo: function(callback){
if (!callback){
throw "callback is required";
}
async(function(){
callback({
firstName: 'Homer',
lastName: 'Simpson',
email: '[email protected]',
address: userAddress
})
})
},
makeRequest: function(endpoint, args){
var url = endpoint.replace(/http[s]?:\/\//gi, "http://www.corsproxy.com/");
ajax(url, args)
},
installApp: function(url, callback){
async(function(){
callback(null, true)
})
},
getApplication: function(appId, callback){
async(function(){
callback({id: appId, version: '1.0.0'})
})
},
userStringForSatoshi: function(satoshiAmount) {
var amount = satoshiAmount / bitcoinFormatToSatoshi[preferredBitcoinFormat]
return amount.toLocaleString()
},
satoshiFromUserString: function(formattedAmount) {
var floatValue = bitcoin.valueFromUserString(formattedAmount)
return floatValue * bitcoinFormatToSatoshi[preferredBitcoinFormat]
},
userStringForCurrencyValue: function(amount, currency) {
currency = currency || preferredCurrency
var maximumFractionDigits = currency == 'JPY' ? 0 : 2
return parseFloat(amount.toFixed(maximumFractionDigits)).toLocaleString()
},
valueFromUserString: function(formattedAmount) {
var thousandsSeparator = (1000).toLocaleString().substring(1, 2)
return parseFloat(formattedAmount.replace(thousandsSeparator, ''))
}
};
}