This repository has been archived by the owner on Feb 26, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 678
/
to.js
86 lines (72 loc) · 1.77 KB
/
to.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
var utils = require("ethereumjs-util");
module.exports = {
// Note: Do not use to.hex() when you really mean utils.addHexPrefix().
hex: function(val) {
if (typeof val == "string") {
if (val === "0x") {
return "0x0";
} else if (val.indexOf("0x") == 0) {
return val;
} else {
val = new utils.BN(val);
}
}
if (typeof val == "boolean") {
val = val ? 1 : 0;
}
if (typeof val == "number") {
val = utils.intToHex(val);
}
// Support Buffer, BigInteger and BN library
// Hint: BN is used in ethereumjs
if (typeof val == "object") {
val = val.toString("hex");
if (val == "") {
val = "0";
}
}
return utils.addHexPrefix(val);
},
rpcQuantityHexString: function(val) {
val = this.hex(val);
val = "0x" + val.replace("0x", "").replace(/^0+/, "");
if (val == "0x") {
val = "0x0";
}
return val;
},
rpcDataHexString: function(val, length) {
if(typeof(length) == "number") {
val = this.hex(val).replace("0x", "");
val = new Array(length-val.length).fill("0").join("") + val;
} else {
if(val.length == 0) {
return "0x";
}
val = this.hex(val).replace("0x", "");
if(val.length % 2 != 0) {
val = "0" + val;
}
}
return "0x" + val;
},
hexWithZeroPadding: function(val) {
val = this.hex(val);
const digits = val.replace("0x", "");
if (digits.length & 0x1) {
return "0x0" + digits;
}
return val;
},
number: function(val) {
if (typeof val == "number") {
return val;
}
if (typeof val == "string") {
if (val.indexOf('0x') != 0) {
return parseInt(val)
}
}
return utils.bufferToInt(utils.toBuffer(val));
}
};