-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathutils.js
79 lines (72 loc) · 1.8 KB
/
utils.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
require("dotenv").config();
function remove0x(str) {
if (str.startsWith("0x")) {
return str.slice(2);
}
return str;
}
function add0x(str) {
if (!str.startsWith("0x")) {
return "0x" + str;
}
return str;
}
function sendRawTx(web3, raw) {
return new Promise((resolve, reject) => {
web3.currentProvider.send({
jsonrpc: '2.0',
method: 'eth_sendRawTransaction',
params: [raw],
id: 1
}, (e, r) => {
if (e) {
console.log("error:");
console.log(e);
reject(e);
}
else if (r.error !== undefined) {
console.log("error");
console.log(r.error);
reject(r.error)
}
else {
//console.log(r);
resolve(r.result);
}
});
});
}
function getJSONInterface(web3, abi, type, name) {
return new Promise((resolve, reject) => {
let res = abi.find((x) => {
return (x.type === type && x.name === name);
})
if (res === undefined) {
console.log(name + " with type " + type + " was not found in supplied JSON-ABI");
reject(undefined);
}
resolve(res);
});
}
function getSHA256hash(message) {
return new Promise((resolve, reject) => {
try {
const SHA256 = require("crypto-js/sha256");
let hash = SHA256(message);
resolve(hash.toString());
} catch (err) {
reject(err);
}
});
}
function addEnvWallet(web3) {
web3.eth.accounts.wallet.add(process.env.PRIVATE_KEY);
}
module.exports = {
remove0x,
add0x,
sendRawTx,
getJSONInterface,
getSHA256hash,
addEnvWallet
}