forked from status-im/status-legacy
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathweb3_init.js
More file actions
121 lines (101 loc) · 3.14 KB
/
web3_init.js
File metadata and controls
121 lines (101 loc) · 3.14 KB
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
if(typeof StatusHttpProvider === "undefined"){
var callbackId = 0;
var callbacks = {};
function httpCallback(id, data) {
var result = data;
var error = null;
try {
result = JSON.parse(data);
} catch (e) {
error = {message: "InvalidResponse"};
}
if (callbacks[id]) {
callbacks[id](error, result);
}
}
var StatusHttpProvider = function (host, timeout) {
this.host = host || 'http://localhost:8545';
this.timeout = timeout || 0;
};
StatusHttpProvider.prototype.send = function (payload) {
if (typeof StatusBridge == "undefined") {
if (window.location.protocol == "https:") {
throw new Error('You tried to send "' + payload.method + '" synchronously. Synchronous requests are not supported, sorry.');
}
var request = this.prepareRequest(false);
try {
request.send(JSON.stringify(payload));
} catch (error) {
throw errors.InvalidConnection(this.host);
}
var result = request.responseText;
try {
result = JSON.parse(result);
} catch (e) {
throw errors.InvalidResponse(request.responseText);
}
return result;
} else {
result = StatusBridge.sendRequestSync(this.host, JSON.stringify(payload));
try {
result = JSON.parse(result);
} catch (e) {
throw new Error("InvalidResponse: " + result);
}
return result;
}
};
StatusHttpProvider.prototype.prepareRequest = function () {
var request = new XMLHttpRequest();
request.open('POST', this.host, false);
request.setRequestHeader('Content-Type', 'application/json');
return request;
};
function sendAsync(payload, callback) {
var messageId = callbackId++;
callbacks[messageId] = callback;
if (typeof StatusBridge == "undefined") {
var data = {
payload: JSON.stringify(payload),
callbackId: JSON.stringify(messageId),
host: this.host
};
webkit.messageHandlers.sendRequest.postMessage(JSON.stringify(data));
} else {
StatusBridge.sendRequest(this.host, JSON.stringify(messageId), JSON.stringify(payload));
}
};
StatusHttpProvider.prototype.sendAsync = sendAsync;
/**
* Synchronously tries to make Http request
*
* @method isConnected
* @return {Boolean} returns true if request haven't failed. Otherwise false
*/
StatusHttpProvider.prototype.isConnected = function () {
try {
this.sendAsync({
id: 9999999999,
jsonrpc: '2.0',
method: 'net_listening',
params: []
}, function () {
});
return true;
} catch (e) {
return false;
}
};
}
var protocol = window.location.protocol
var address = providerAddress || "http://localhost:8545";
console.log(protocol);
if (typeof web3 === "undefined") {
if (protocol == "https:") {
console.log("StatusHttpProvider");
web3 = new Web3(new StatusHttpProvider(address));
} else if (protocol == "http:") {
console.log("HttpProvider");
web3 = new Web3(new Web3.providers.HttpProvider(address));
}
}