-
Notifications
You must be signed in to change notification settings - Fork 14
/
utils.js
132 lines (111 loc) · 4.39 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
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
const ethUtils = require('ethereumjs-util');
const EthCrypto = require('eth-crypto');
const web3Utils = require('web3-utils')
const relay_prefix = "rlx:"
function toUint256_noPrefix(int) {
return removeHexPrefix(ethUtils.bufferToHex(ethUtils.setLengthLeft(int, 32)));
}
function removeHexPrefix(hex) {
return hex.replace(/^0x/, '');
}
const zeroPad = "0000000000000000000000000000000000000000000000000000000000000000"
function padTo64(hex) {
if (hex.length < 64) {
hex = (zeroPad + hex).slice(-64);
}
return hex;
}
function bytesToHex_noPrefix(bytes) {
let hex = removeHexPrefix(web3Utils.toHex(bytes))
if (hex.length % 2 != 0) {
hex = "0" + hex;
}
return hex
}
module.exports = {
register_new_relay: async function (relayHub, stake, delay, txFee, url, account) {
await relayHub.stake(account, delay, {from: account, value: stake})
return await relayHub.registerRelay(txFee, url, {from: account})
},
getTransactionHash: function (from, to, tx, txfee, gas_price, gas_limit, nonce, relay_hub_address, relay_address) {
let txhstr = bytesToHex_noPrefix(tx)
let dataToHash =
Buffer.from(relay_prefix).toString("hex") +
removeHexPrefix(from)
+ removeHexPrefix(to)
+ txhstr
+ toUint256_noPrefix(parseInt(txfee))
+ toUint256_noPrefix(parseInt(gas_price))
+ toUint256_noPrefix(parseInt(gas_limit))
+ toUint256_noPrefix(parseInt(nonce))
+ removeHexPrefix(relay_hub_address)
+ removeHexPrefix(relay_address)
return web3Utils.sha3('0x'+dataToHash )
},
getTransactionSignature: async function (web3, account, hash) {
let sig_
try {
sig_ = await new Promise((resolve, reject) => {
try {
web3.eth.personal.sign(hash, account, (err, res) => {
if (err) reject(err)
else resolve(res)
})
} catch (e) {
reject(e)
}
})
} catch (e) {
sig_ = await new Promise((resolve, reject) => {
web3.eth.sign(hash, account, (err, res) => {
if (err) reject(err)
else resolve(res)
})
})
}
let signature = ethUtils.fromRpcSig(sig_);
let sig = web3Utils.bytesToHex(signature.r) + removeHexPrefix(web3Utils.bytesToHex(signature.s)) + removeHexPrefix(web3Utils.toHex(signature.v));
return sig;
},
getTransactionSignatureWithKey: function(privKey, hash, withPrefix=true) {
let signed
if (withPrefix){
let msg = Buffer.concat([Buffer.from("\x19Ethereum Signed Message:\n32"), Buffer.from(removeHexPrefix(hash), "hex")])
signed = web3Utils.sha3("0x"+msg.toString('hex') )
}
else
signed = hash
let keyHex = "0x" + Buffer.from(privKey).toString('hex')
const sig_ = EthCrypto.sign(keyHex, signed)
let signature = ethUtils.fromRpcSig(sig_);
let sig = web3Utils.bytesToHex(signature.r) + removeHexPrefix(web3Utils.bytesToHex(signature.s)) + removeHexPrefix(web3Utils.toHex(signature.v));
return sig
},
getEcRecoverMeta: function(message, signature) {
if (typeof signature === 'string'){
let r = this.parseHexString(signature.substr(2, 65))
let s = this.parseHexString(signature.substr(66, 65))
let v = this.parseHexString(signature.substr(130,2))
signature = {
v: v,
r: r,
s: s
}
}
let msg = Buffer.concat([Buffer.from("\x19Ethereum Signed Message:\n32"), Buffer.from(removeHexPrefix(message), "hex")]);
let signed = web3Utils.sha3("0x"+msg.toString('hex'));
let buf_signed = Buffer.from(removeHexPrefix(signed), "hex");
let signer = ethUtils.bufferToHex(ethUtils.pubToAddress(ethUtils.ecrecover(buf_signed, signature.v, signature.r, signature.s)));
return signer;
},
parseHexString: function(str) {
var result = [];
while (str.length >= 2) {
result.push(parseInt(str.substring(0, 2), 16));
str = str.substring(2, str.length);
}
return result;
},
removeHexPrefix: removeHexPrefix,
padTo64: padTo64
}