-
Notifications
You must be signed in to change notification settings - Fork 3
/
utils.go
158 lines (146 loc) · 3.79 KB
/
utils.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
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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
package simpleBlockchain
import (
"crypto/sha256"
"encoding/binary"
"encoding/hex"
"errors"
"math"
"os"
"reflect"
)
func DecodeVarint(data []byte) (int, uint, error){
switch size := data[0]; {
case size < 253:
return 1, uint(size), nil
case size == 253:
num := binary.LittleEndian.Uint16(data[1:3])
return 3, uint(num), nil
case size == 254:
num := binary.LittleEndian.Uint32(data[1:5])
return 5, uint(num), nil
case size == 255:
num := binary.LittleEndian.Uint64(data[1:9])
return 9, uint(num), nil
default:
return 0, 0, errors.New("can't decode this")
}
}
func DecodeVarIntForIndex(input []byte) (n int, pos int) {
for {
data := input[pos]
pos += 1
n = (n << 7) | int(data & 0x7f)
if data & 0x80 == 0 {
return n, pos
}
n += 1
}
}
func EncodeVarint(pkScriptBytes uint) ([]byte, error){
switch {
case pkScriptBytes >= 0 && pkScriptBytes < 253 :
return IntToLittleEndianBytes(uint8(pkScriptBytes)), nil
case pkScriptBytes >= 253 && pkScriptBytes <= math.MaxUint16:
bytes := IntToLittleEndianBytes(uint16(pkScriptBytes))
result := AddHeadSlice([]byte{0xfd},bytes)
return result, nil
case pkScriptBytes > math.MaxUint16 && pkScriptBytes <= math.MaxUint32:
bytes := IntToLittleEndianBytes(uint32(pkScriptBytes))
result := AddHeadSlice([]byte{0xfe},bytes)
return result, nil
case pkScriptBytes > math.MaxUint32 && pkScriptBytes <= math.MaxUint64:
bytes := IntToLittleEndianBytes(uint64(pkScriptBytes))
result := AddHeadSlice([]byte{0xff},bytes)
return result, nil
default:
return nil, errors.New("not in range")
}
}
func AddHeadSlice(head []byte, slice[]byte) []byte{
result := make([]byte, len(head)+len(slice))
copy(result[0:len(head)], head)
copy(result[len(head):], slice)
return result
}
func CutBytes(input []byte, index int) ([]byte, []byte) {
result := input[0:index]
input = input[index:]
return result, input
}
func ConcatCopy(slices ...[]byte) []byte {
var totalLen int
for _, s := range slices {
totalLen += len(s)
}
result := make([]byte, totalLen)
var i int
for _, s := range slices {
i += copy(result[i:], s)
}
return result
}
func ReverseBytes(input []byte) []byte{
temp := make([]byte, len(input))
copy(temp, input)
for i, j := 0, len(temp)-1 ; i < j ; i, j = i+1, j-1{
temp[i], temp[j] = temp[j], temp[i]
}
return temp
}
func DoubleSha256(input []byte) []byte {
sha := sha256.Sum256(input)
doubleSha:= sha256.Sum256(sha[:])
return doubleSha[:]
}
func IntToLittleEndianBytes(input interface{}) []byte{
switch t, v := reflect.TypeOf(input), reflect.ValueOf(input); t.Kind() {
case reflect.Uint8:
return []byte{v.Interface().(uint8)}
case reflect.Uint16:
buf := make([]byte, 2)
binary.LittleEndian.PutUint16(buf, v.Interface().(uint16))
return buf
case reflect.Uint32:
buf := make([]byte, 4)
binary.LittleEndian.PutUint32(buf, v.Interface().(uint32))
return buf
case reflect.Uint64:
buf := make([]byte, 8)
binary.LittleEndian.PutUint64(buf, v.Interface().(uint64))
return buf
case reflect.Int32:
buf := make([]byte, 4)
binary.LittleEndian.PutUint32(buf, uint32(v.Interface().(int32)))
return buf
case reflect.Int64:
buf := make([]byte, 8)
binary.LittleEndian.PutUint64(buf, uint64(v.Interface().(int64)))
return buf
case reflect.Int:
buf := make([]byte, 8)
binary.LittleEndian.PutUint64(buf, uint64(v.Interface().(int)))
return buf
default:
panic(errors.New("input type is not supported"))
}
}
func IsFileExists(filename string) bool {
info, err := os.Stat(filename)
if os.IsNotExist(err) {
return false
}
return !info.IsDir()
}
func HexStrToBytes(hexStr string) []byte {
if len(hexStr) == 0 {
return []byte{}
}
if len(hexStr) >= 2 && hexStr[0:2] == "0x" {
hexStr = hexStr[2:]
}
if len(hexStr)%2 != 0 {
hexStr = "0" + hexStr
}
hexBytes, _:= hex.DecodeString(hexStr)
return hexBytes
}