This repository has been archived by the owner on Jun 1, 2023. It is now read-only.
/
utils.cjs
116 lines (101 loc) · 3.26 KB
/
utils.cjs
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
const BN = require('bn.js')
const Decimal = require('decimal.js')
const DNA_BASE = '1000000000000000000'
Decimal.set({toExpPos: 10000})
function argToBytes(data) {
try {
switch (data.format) {
case 'byte': {
const val = parseInt(data.value, 10)
if (val >= 0 && val <= 255) {
return [val]
}
throw new Error('invalid byte value')
}
case 'int8': {
const val = parseInt(data.value, 10)
if (val >= 0 && val <= 255) {
return [val]
}
throw new Error('invalid int8 value')
}
case 'uint64': {
const res = new BN(data.value)
if (res.isNeg()) throw new Error('invalid uint64 value')
const arr = res.toArray('le')
return [...arr, ...new Array(8).fill(0)].slice(0, 8)
}
case 'int64': {
const arr = new BN(data.value).toArray('le')
return [...arr, ...new Array(8).fill(0)].slice(0, 8)
}
case 'string': {
return [...Buffer.from(data.value, 'utf8')]
}
case 'bigint': {
return new BN(data.value).toArray()
}
case 'hex': {
return [...hexToUint8Array(data.value)]
}
case 'dna': {
return new BN(
new Decimal(data.value).mul(new Decimal(DNA_BASE)).toString()
).toArray()
}
default: {
return [...hexToUint8Array(data.value)]
}
}
} catch (e) {
throw new Error(
`cannot parse ${data.format} at index ${data.index}: ${e.message}`
)
}
}
function hexToUint8Array(hexString) {
const str = stripHexPrefix(hexString)
const arrayBuffer = new Uint8Array(str.length / 2)
for (let i = 0; i < str.length; i += 2) {
const byteValue = parseInt(str.substr(i, 2), 16)
arrayBuffer[i / 2] = byteValue
}
return arrayBuffer
}
function buildDynamicArgs(args = []) {
return args
.map(({format = 'hex', value}, index) => ({
index,
format,
value: typeof value !== 'string' ? value?.toString() ?? null : value,
}))
.filter(({value = null}) => value !== null)
}
function argsToSlice(args) {
if (args?.length === 0) return []
const maxIndex = Math.max(...args.map(x => x.index))
const result = new Array(maxIndex).fill(null)
args.forEach(element => {
result[element.index] = argToBytes(element)
})
return result
}
function stripHexPrefix(str) {
if (typeof str !== 'string') {
return str
}
return isHexPrefixed(str) ? str.slice(2) : str
}
function isHexPrefixed(str) {
return str.slice(0, 2) === '0x'
}
function toHexString(byteArray, withPrefix) {
return (
(withPrefix ? '0x' : '') +
Array.from(byteArray, function(byte) {
// eslint-disable-next-line no-bitwise
return `0${(byte & 0xff).toString(16)}`.slice(-2)
}).join('')
)
}
module.exports = { argsToSlice, buildDynamicArgs, hexToUint8Array, toHexString }