-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
114 lines (100 loc) · 3.71 KB
/
index.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
(function Web3SwiftJS() {
require("./wk.bridge");
const ZeroClientProvider = require('web3-provider-engine/zero.js');
const Web3 = require("web3");
const getAccountsProxified = function(cb) {
console.log("JS bridging accounts access");
if (typeof window !== 'undefined' && window.bridge) {
window.bridge.post('eth_getAccounts', {}, (parameters, error) => {
if (error) {
cb(error.description, null);
}
cb(null, parameters['accounts']);
});
} else {
console.log("No bridge to native code is found");
cb(true, null);
}
}
const getCoinbaseProxified = function(cb) {
console.log("JS bridging coinbase access");
if (typeof window !== 'undefined' && window.bridge) {
window.bridge.post('eth_coinbase', {}, (parameters, error) => {
if (error) {
cb(error.description, null);
}
cb(null, parameters['coinbase']);
});
} else {
console.log("No bridge to native code is found");
cb(true, null);
}
}
const getRPCurl = function(cb) {
console.log("JS bridging rpc url access");
if (typeof window !== 'undefined' && window.bridge) {
window.bridge.post('getRPCurl', {}, (parameters, error) => {
if (error) {
cb(error.description, null);
}
cb(null, parameters['rpcURL']);
});
} else {
console.log("No bridge to native code is found");
cb(true, null);
}
}
const signTransactionProxified = function(tx, cb) {
console.log("JS bridging sign transaction access");
if (typeof window !== 'undefined' && window.bridge) {
window.bridge.post('eth_signTransaction', {transaction: tx}, (parameters, error) => {
if (error) {
cb(error.description, null);
}
cb(null, parameters['signedTransaction']);
});
} else {
console.log("No bridge to native code is found");
cb(true, null);
}
}
const signPersonalMessageProxified = function(payload, cb) {
console.log("JS bridging sign message access");
if (typeof window !== 'undefined' && window.bridge) {
window.bridge.post('eth_sign', {payload}, (parameters, error) => {
if (error) {
cb(error.description, null);
}
const result = parameters['signedMessage'];
cb(null, result);
});
} else {
console.log("No bridge to native code is found");
cb(true, null);
}
}
getRPCurl(function(err, rpcUrl) {
if (err) {
console.log("Can not init web3 and proxy");
return;
}
console.log("Using RPC URL " + rpcUrl);
const engine = ZeroClientProvider({
getAccounts: getAccountsProxified,
signTransaction: signTransactionProxified,
signMessage: signPersonalMessageProxified,
signPersonalMessage: signPersonalMessageProxified,
rpcUrl: rpcUrl,
});
window.Web3 = Web3;
const web3 = new Web3();
web3.setProvider(engine);
if (typeof window !== 'undefined') {
window.web3 = web3;
}
// log new blocks
engine.on('block', function(block){
console.log('BLOCK CHANGED:', '#'+block.number.toString('hex'), '0x'+block.hash.toString('hex'))
})
});
}());