forked from hyperledger/iroha-javascript
-
Notifications
You must be signed in to change notification settings - Fork 0
/
integrationHelpers.ts
57 lines (49 loc) · 1.81 KB
/
integrationHelpers.ts
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
import createKeccakHash from 'keccak'
function pad (num: string, size: number, dir: string) {
while (num.length < size) {
if (dir === 'left') {
num = '0' + num
} else if (dir === 'right') {
num = num + '0'
}
}
return num
}
function hex (str: string) {
const arr1: Array<string> = []
for (let n = 0, l = str.length; n < l; n++) {
const hex = Number(str.charCodeAt(n)).toString(16)
arr1.push(hex)
}
return arr1.join('')
}
function hexToAscii (str: string) {
const hex = str.toString()
let asciiStr = ''
for (let n = 0; n < hex.length; n += 2) {
asciiStr += String.fromCharCode(parseInt(hex.substr(n, 2), 16))
}
return asciiStr
}
function makeNumberHexLeftPadded (number: number, width = 64) {
const numberHex = number.toString(16)
return pad(numberHex, width, 'left')
}
function leftPaddedAddressOfParam (paramIndex: number, numberOfParams: number, width = 64) {
/* Specifies the position of each argument according to Contract ABI specifications. */
const bitsOffset = 32 * numberOfParams
const bitsPerParam = 64
const bitsForTheParam = bitsOffset + bitsPerParam * paramIndex
return makeNumberHexLeftPadded(bitsForTheParam, width)
}
function getFirstFourBytesOfKeccak (functionSignature: string) {
return createKeccakHash('keccak256').update(functionSignature).digest('hex').slice(0, 8)
}
function argumentEncoding (arg: string) {
/* Encodes the argument according to Contract ABI specifications. */
let encodedArgument: string = pad((arg.length).toString(16), 64, 'left')
const encodedString = hex(Buffer.from(arg, 'utf-8').toString())
encodedArgument = encodedArgument + pad(encodedString, 64, 'right').toUpperCase()
return encodedArgument
}
export { makeNumberHexLeftPadded, leftPaddedAddressOfParam, getFirstFourBytesOfKeccak, argumentEncoding, hexToAscii }