-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathutils.go
70 lines (59 loc) · 1.12 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
package script
import (
"crypto/sha256"
"encoding/binary"
"golang.org/x/crypto/ripemd160"
)
func GetHash160(data []byte) (hash []byte) {
sha := sha256.New()
sha.Write(data[:])
tmp := sha.Sum(nil)
rp := ripemd160.New()
rp.Write(tmp)
hash = rp.Sum(nil)
return
}
func SafeDecodeVarIntForScript(raw []byte) (cnt uint, cnt_size uint) {
if len(raw) < 1 {
return 0, 0
}
if raw[0] < OP_PUSHDATA1 {
return uint(raw[0]), 1
}
if raw[0] == OP_PUSHDATA1 {
if len(raw) < 2 {
return 0, 0
}
return uint(raw[1]), 2
} else if raw[0] == OP_PUSHDATA2 {
if len(raw) < 3 {
return 0, 0
}
return uint(binary.LittleEndian.Uint16(raw[1:3])), 3
} else if raw[0] == OP_PUSHDATA4 {
if len(raw) < 5 {
return 0, 0
}
return uint(binary.LittleEndian.Uint32(raw[1:5])), 5
}
return 0, 0
}
func getVarIntLen(length int) int {
res := 0
if length <= 0x4b {
res = 0
} else if length <= 0xff {
res = 1
} else if length <= 0xffff {
res = 2
} else {
res = 4
}
return res
}
func ReverseBytesInPlace(data []byte) {
n := len(data)
for i := 0; i < n/2; i++ {
data[i], data[n-1-i] = data[n-1-i], data[i]
}
}