/
bitski_subprovider.js
148 lines (132 loc) · 4.65 KB
/
bitski_subprovider.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
const ethers = require('ethers');
const { BigNumber } = ethers;
const Bitski = require("bitski-node");
const {
log
} = require('./utils');
const BitskiSubProvider = function(clientID, credentialID, secret, accounts, chainId, config) {
let network = 'mainnet';
if(chainId === '4') {
network = 'rinkeby';
} else if (chainId === '42') {
network = 'kovan';
} // else if (chainId == 3) {
// network = 'ropsten';
// }
this.lastId = 0;
const options = {
credentials: {
id: credentialID,
secret,
},
network,
debug: config.debug,
disableBlockTracking: true,
};
// Pass options with the provider
this.bitskiProvider = Bitski.getProvider(clientID, options);
// this.bitskiProvider._blockTracker.stop();
this.accounts = accounts;
}
BitskiSubProvider.prototype.setEngine = function(engine) {
this.engine = engine;
}
BitskiSubProvider.prototype.fetchGasPrice = function() {
const self = this;
return new Promise((resolve, reject) => {
self.engine.sendAsync({id: ++this.lastId, method: 'eth_gasPrice', jsonrpc: '2.0'}, (error, json) =>{
if(error) {
reject(error);
} else {
resolve(json.result);
}
});
})
}
BitskiSubProvider.prototype.fetchNonce = function(from) {
const self = this;
return new Promise((resolve, reject) => {
self.engine.sendAsync({ id: ++this.lastId, method: 'eth_getTransactionCount', params: [from, 'latest'], jsonrpc: '2.0'}, (error, json) =>{
if(error) {
reject(error);
} else {
resolve(json.result);
}
});
})
}
BitskiSubProvider.prototype.fetchBalance = function(from) {
const self = this;
return new Promise((resolve, reject) => {
self.engine.sendAsync({ id: ++self.lastId, method: 'eth_getBalance', params: [from, 'latest'], jsonrpc: '2.0'}, (error, json) =>{
if(error) {
reject(error);
} else {
resolve(json.result);
}
});
})
}
BitskiSubProvider.prototype.handleRequest = async function(payload, next, end) {
const self = this;
if (payload.method === 'eth_accounts' && this.accounts) {
return end(null, this.accounts);
} else if (payload.method === 'eth_sendTransaction') {
const rawTx = payload.params[0];
const from = rawTx.from;
if(!rawTx.gas) {
return end(new Error('gas not specified'));
}
const gasPrice = BigNumber.from(rawTx.gasPrice || await this.fetchGasPrice());
const gas = BigNumber.from(rawTx.gas);
const balance = BigNumber.from(await this.fetchBalance(from));
const value = BigNumber.from(rawTx.value || 0);
const balanceRequired = (gasPrice.mul(gas)).add(value);
// console.log({
// gas: gas.toString(),
// value: value.toString(),
// gasPrice: gasPrice.toString(),
// balanceRequired: balanceRequired.toString(),
// balance: balance.toString(),
// })
if(balance.lt(balanceRequired)) {
return end(new Error('Not enough balance: '
+ balanceRequired.toString()
+ '( ' + gas.toString() + ' gas x ' + gasPrice.toString() + ' gasPrice'
+ ' ) > ' + balance.toString()));
}
const nonce = await this.fetchNonce(from);
let result;
try{
rawTx.gasPrice = gasPrice.toHexString();
rawTx.value = value.toHexString();
rawTx.gas = gas.toHexString();
rawTx.nonce = nonce;
// console.log(JSON.stringify(rawTx, null, ' '));
result = await this.sendTransaction(rawTx);
}catch(e) {
return end(e);
}
return end(null, result);
} else if(payload.method == 'eth_sign') {
let signedMessage;
try{
signedMessage = await this.signMessage(payload.params[0], payload.params[1]);
} catch(e) {
return end(e);
}
return end(null, signedMessage);
} else {
next();
}
}
BitskiSubProvider.prototype.signTransaction = async function(from, rawTx) {
return this.bitskiProvider.send('eth_signTransaction', [rawTx]);
}
BitskiSubProvider.prototype.sendTransaction = async function(tx) {
return this.bitskiProvider.send('eth_sendTransaction', [tx]);
}
BitskiSubProvider.prototype.signMessage = function(from, message) {
throw new Error('rocketh TODO: sign Message not implemented in bitski provider');
}
module.exports = BitskiSubProvider;