-
Notifications
You must be signed in to change notification settings - Fork 3
/
tools.js
52 lines (44 loc) · 1.2 KB
/
tools.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
const OpenTimestamps = require('javascript-opentimestamps');
// OpenTimestamps shortcuts
// const Timestamp = OpenTimestamps.Timestamp;
// const Ops = OpenTimestamps.Ops;
const Utils = OpenTimestamps.Utils;
// Const Notary = OpenTimestamps.Notary;
// Convert a hex string to a buffer
exports.hexToString = function (hex) {
return Buffer.from(hex, 'hex');
};
// Convert a byte array to a hex string
exports.bytesToHex = function (bytes) {
const hex = [];
for (let i = 0; i < bytes.length; i++) {
hex.push((bytes[i] >>> 4).toString(16));
hex.push((bytes[i] & 0xF).toString(16));
}
return hex.join('');
};
exports.arrEq = function (arr1, arr2) {
return Utils.arrEq(arr1, arr2);
};
exports.hardFail = function (promise) {
return new Promise((resolve, reject) => {
promise
.then(resolve)
.catch(reject);
});
};
exports.softFail = function (promise) {
return new Promise(resolve => {
promise
.then(resolve)
.catch(resolve);
});
};
// Convert a hex string to a byte array
exports.hexToBytes = function (hex) {
const bytes = [];
for (let c = 0; c < hex.length; c += 2) {
bytes.push(parseInt(hex.substr(c, 2), 16));
}
return bytes;
};