Repository navigation
Expand file tree
/
Copy pathutils.js
More file actions
62 lines (61 loc) · 1.67 KB
/
Copy pathutils.js
File metadata and controls
62 lines (61 loc) · 1.67 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
import { bufferToInt } from 'ethereumjs-util';
const getBufferFromHex = hex => {
hex = sanitizeHex(hex);
const _hex = hex.toLowerCase().replace('0x', '');
return new Buffer(_hex, 'hex');
};
const padLeftEven = hex => {
hex = hex.length % 2 != 0 ? '0' + hex : hex;
return hex;
};
const sanitizeHex = hex => {
hex = hex.substring(0, 2) == '0x' ? hex.substring(2) : hex;
if (hex == '') return '';
return '0x' + padLeftEven(hex);
};
const bufferToHex = buffer => {
return '0x' + buffer.toString('hex');
};
const getHexTxObject = tx => {
return {
to: sanitizeHex(tx.to.toString('hex')),
value: sanitizeHex(tx.value.toString('hex')),
data: sanitizeHex(tx.data.toString('hex')),
chainId: tx.getChainId(),
nonce: sanitizeHex(tx.nonce.toString('hex')),
gasLimit: sanitizeHex(tx.gasLimit.toString('hex')),
gasPrice: sanitizeHex(tx.gasPrice.toString('hex'))
};
};
const getSignTransactionObject = tx => {
return {
rawTransaction: bufferToHex(tx.serialize()),
tx: {
nonce: bufferToHex(tx.nonce),
gasPrice: bufferToHex(tx.gasPrice),
gas: tx.gasLimit ? bufferToHex(tx.gasLimit) : bufferToHex(tx.gas),
to: bufferToHex(tx.to),
value: bufferToHex(tx.value),
input: bufferToHex(tx.data),
v: bufferToHex(tx.v),
r: bufferToHex(tx.r),
s: bufferToHex(tx.s),
hash: bufferToHex(tx.hash())
}
};
};
const calculateChainIdFromV = v => {
const sigV = bufferToInt(v);
let chainId = Math.floor((sigV - 35) / 2);
if (chainId < 0) chainId = 0;
return chainId;
};
export {
getBufferFromHex,
bufferToHex,
getSignTransactionObject,
sanitizeHex,
padLeftEven,
getHexTxObject,
calculateChainIdFromV
};