-
Notifications
You must be signed in to change notification settings - Fork 11
/
txUtils.go
80 lines (69 loc) · 1.76 KB
/
txUtils.go
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
package hcTransaction
import (
"encoding/binary"
"encoding/hex"
"errors"
)
func byteArrayCompare(a, b []byte) bool {
if len(a) != len(b) {
return false
}
for index := 0; index < len(a); index++ {
if a[index] != b[index] {
return false
}
}
return true
}
//reverseBytes endian reverse
func reverseBytes(s []byte) []byte {
for i, j := 0, len(s)-1; i < j; i, j = i+1, j-1 {
s[i], s[j] = s[j], s[i]
}
return s
}
//reverseHexToBytes decode a hex string to an byte array,then change the endian
func reverseHexToBytes(hexVar string) ([]byte, error) {
if len(hexVar)%2 == 1 {
return nil, errors.New("Invalid TxHash!")
}
ret, err := hex.DecodeString(hexVar)
if err != nil {
return nil, err
}
return reverseBytes(ret), nil
}
//reverseBytesToHex change the endian of the input byte array then encode it to hex string
func reverseBytesToHex(bytesVar []byte) string {
return hex.EncodeToString(reverseBytes(bytesVar))
}
//uint16ToLittleEndianBytes
func uint16ToLittleEndianBytes(data uint16) []byte {
tmp := [2]byte{}
binary.LittleEndian.PutUint16(tmp[:], data)
return tmp[:]
}
//littleEndianBytesToUint16
func littleEndianBytesToUint16(data []byte) uint16 {
return binary.LittleEndian.Uint16(data)
}
//uint32ToLittleEndianBytes
func uint32ToLittleEndianBytes(data uint32) []byte {
tmp := [4]byte{}
binary.LittleEndian.PutUint32(tmp[:], data)
return tmp[:]
}
//littleEndianBytesToUint32
func littleEndianBytesToUint32(data []byte) uint32 {
return binary.LittleEndian.Uint32(data)
}
//uint64ToLittleEndianBytes
func uint64ToLittleEndianBytes(data uint64) []byte {
tmp := [8]byte{}
binary.LittleEndian.PutUint64(tmp[:], data)
return tmp[:]
}
//littleEndianBytesToUint64
func littleEndianBytesToUint64(data []byte) uint64 {
return binary.LittleEndian.Uint64(data)
}