-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathuiFuncs.js
executable file
·66 lines (58 loc) · 1.88 KB
/
uiFuncs.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
'use strict';
var uiFuncs = function() {}
var isNumeric = function(n) {
return !isNaN(parseFloat(n)) && isFinite(n);
}
uiFuncs.isTxDataValid = function(txData) {
if (txData.to != "0xCONTRACT" && !ethFuncs.validateEtherAddress(txData.to)) throw 'ERROR_6';
else if (!isNumeric(txData.value) || parseFloat(txData.value) < 0) throw 'ERROR_8';
else if (!isNumeric(txData.gasLimit) || parseFloat(txData.gasLimit) <= 0) throw 'ERROR_9';
else if (!ethFuncs.validateHexString(txData.data)) throw 'ERROR_10';
if (txData.to == "0xCONTRACT") txData.to = '';
}
uiFuncs.generateTx = function(txData, callback) {
try {
uiFuncs.isTxDataValid(txData);
ajaxReq.getTransactionData(txData.from, function(data) {
if (data.error) throw data.msg;
data = data.data;
var rawTx = {
nonce: ethFuncs.sanitizeHex(data.nonce),
gasPrice: ethFuncs.sanitizeHex(ethFuncs.addTinyMoreToGas(data.gasprice)),
gasLimit: ethFuncs.sanitizeHex(ethFuncs.decimalToHex(txData.gasLimit)),
to: ethFuncs.sanitizeHex(txData.to),
value: ethFuncs.sanitizeHex(ethFuncs.decimalToHex(etherUnits.toWei(txData.value, txData.unit))),
data: ethFuncs.sanitizeHex(txData.data)
}
var eTx = new ethUtil.Tx(rawTx);
eTx.sign(new Buffer(txData.key, 'hex'));
rawTx.rawTx = JSON.stringify(rawTx);
rawTx.signedTx = '0x' + eTx.serialize().toString('hex');
rawTx.isError = false;
if (callback !== undefined) callback(rawTx);
});
} catch (e) {
if (callback !== undefined) callback({
isError: true,
error: e
});
}
}
uiFuncs.sendTx = function(signedTx, additional_data, callback) {
ajaxReq.sendRawTx(signedTx, additional_data, function(data) {
var resp = {};
if (data.error) {
resp = {
isError: true,
error: data.msg
};
} else {
resp = {
isError: false,
data: data.data
};
}
if (callback !== undefined) callback(resp);
});
}
module.exports = uiFuncs;