-
Notifications
You must be signed in to change notification settings - Fork 66
/
solidity-helper.js
80 lines (60 loc) · 1.32 KB
/
solidity-helper.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
var leftpad = require('leftpad');
var web3utils = require('web3-utils');
module.exports = {
stringToSolidityBytes32(string)
{
var result = "";
var i = 0;
for(i=0;i<32;i++)
{
if(string.length > i)
{
result += string.charCodeAt(i).toString(16)
}else {
result += "00"
}
}
return result;
},
//0x6161000000000000000000000000000000000000000000000000000000000000 -> aa
solidityBytes32ToString(bytes32)
{
var result = "";
var i = 0;
if(bytes32.startsWith('0x'))
{
var rawcodes = bytes32.substring(2);
}else{
var rawcodes = bytes32;
}
for(i=0;i<32;i++)
{
var segment = rawcodes.substring(i*2,i*2+2)
var segment_value = parseInt(segment,16)
if(segment_value != '00')
{
result += String.fromCharCode(segment_value)
}
}
return result;
},
solidityKeccak256(...args) {
args = args.map(arg => {
if (typeof arg === 'string') {
if (arg.substring(0, 2) === '0x') {
return arg.slice(2)
} else {
return web3utils.toHex(arg).slice(2)
}
}
if (typeof arg === 'number') {
if (arg < 0) {
return leftpad((arg >>> 0).toString(16), 64, 'F');
}
return leftpad((arg).toString(16), 64, 0);
}
})
args = args.join('')
return web3utils.sha3(args, { encoding: 'hex' })
}
}