-
Notifications
You must be signed in to change notification settings - Fork 30
/
index.js
203 lines (173 loc) · 5.14 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
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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
const isHexPrefixed = require('is-hex-prefixed');
const stripHexPrefix = require('strip-hex-prefix');
/**
* Pads a `String` to have an even length
* @param {String} value
* @return {String} output
*/
function padToEven(value) {
var a = value; // eslint-disable-line
if (typeof a !== 'string') {
throw new Error(`[ethjs-util] while padding to even, value must be string, is currently ${typeof a}, while padToEven.`);
}
if (a.length % 2) {
a = `0${a}`;
}
return a;
}
/**
* Converts a `Number` into a hex `String`
* @param {Number} i
* @return {String}
*/
function intToHex(i) {
var hex = i.toString(16); // eslint-disable-line
return `0x${hex}`;
}
/**
* Converts an `Number` to a `Buffer`
* @param {Number} i
* @return {Buffer}
*/
function intToBuffer(i) {
const hex = intToHex(i);
return new Buffer(padToEven(hex.slice(2)), 'hex');
}
/**
* Get the binary size of a string
* @param {String} str
* @return {Number}
*/
function getBinarySize(str) {
if (typeof str !== 'string') {
throw new Error(`[ethjs-util] while getting binary size, method getBinarySize requires input 'str' to be type String, got '${typeof str}'.`);
}
return Buffer.byteLength(str, 'utf8');
}
/**
* Returns TRUE if the first specified array contains all elements
* from the second one. FALSE otherwise.
*
* @param {array} superset
* @param {array} subset
*
* @returns {boolean}
*/
function arrayContainsArray(superset, subset, some) {
if (Array.isArray(superset) !== true) { throw new Error(`[ethjs-util] method arrayContainsArray requires input 'superset' to be an array got type '${typeof superset}'`); }
if (Array.isArray(subset) !== true) { throw new Error(`[ethjs-util] method arrayContainsArray requires input 'subset' to be an array got type '${typeof subset}'`); }
return subset[Boolean(some) && 'some' || 'every']((value) => (superset.indexOf(value) >= 0));
}
/**
* Should be called to get utf8 from it's hex representation
*
* @method toUtf8
* @param {String} string in hex
* @returns {String} ascii string representation of hex value
*/
function toUtf8(hex) {
const bufferValue = new Buffer(padToEven(stripHexPrefix(hex).replace(/^0+|0+$/g, '')), 'hex');
return bufferValue.toString('utf8');
}
/**
* Should be called to get ascii from it's hex representation
*
* @method toAscii
* @param {String} string in hex
* @returns {String} ascii string representation of hex value
*/
function toAscii(hex) {
var str = ''; // eslint-disable-line
var i = 0, l = hex.length; // eslint-disable-line
if (hex.substring(0, 2) === '0x') {
i = 2;
}
for (; i < l; i += 2) {
const code = parseInt(hex.substr(i, 2), 16);
str += String.fromCharCode(code);
}
return str;
}
/**
* Should be called to get hex representation (prefixed by 0x) of utf8 string
*
* @method fromUtf8
* @param {String} string
* @param {Number} optional padding
* @returns {String} hex representation of input string
*/
function fromUtf8(stringValue) {
const str = new Buffer(stringValue, 'utf8');
return `0x${padToEven(str.toString('hex')).replace(/^0+|0+$/g, '')}`;
}
/**
* Should be called to get hex representation (prefixed by 0x) of ascii string
*
* @method fromAscii
* @param {String} string
* @param {Number} optional padding
* @returns {String} hex representation of input string
*/
function fromAscii(stringValue) {
var hex = ''; // eslint-disable-line
for(var i = 0; i < stringValue.length; i++) { // eslint-disable-line
const code = stringValue.charCodeAt(i);
const n = code.toString(16);
hex += n.length < 2 ? `0${n}` : n;
}
return `0x${hex}`;
}
/**
* getKeys([{a: 1, b: 2}, {a: 3, b: 4}], 'a') => [1, 3]
*
* @method getKeys get specific key from inner object array of objects
* @param {String} params
* @param {String} key
* @param {Boolean} allowEmpty
* @returns {Array} output just a simple array of output keys
*/
function getKeys(params, key, allowEmpty) {
if (!Array.isArray(params)) { throw new Error(`[ethjs-util] method getKeys expecting type Array as 'params' input, got '${typeof params}'`); }
if (typeof key !== 'string') { throw new Error(`[ethjs-util] method getKeys expecting type String for input 'key' got '${typeof key}'.`); }
var result = []; // eslint-disable-line
for (var i = 0; i < params.length; i++) { // eslint-disable-line
var value = params[i][key]; // eslint-disable-line
if (allowEmpty && !value) {
value = '';
} else if (typeof(value) !== 'string') {
throw new Error('invalid abi');
}
result.push(value);
}
return result;
}
/**
* Is the string a hex string.
*
* @method check if string is hex string of specific length
* @param {String} value
* @param {Number} length
* @returns {Boolean} output the string is a hex string
*/
function isHexString(value, length) {
if (typeof(value) !== 'string' || !value.match(/^0x[0-9A-Fa-f]*$/)) {
return false;
}
if (length && value.length !== 2 + 2 * length) { return false; }
return true;
}
module.exports = {
arrayContainsArray,
intToBuffer,
getBinarySize,
isHexPrefixed,
stripHexPrefix,
padToEven,
intToHex,
fromAscii,
fromUtf8,
toAscii,
toUtf8,
getKeys,
isHexString,
};