This repository has been archived by the owner on Jan 25, 2021. It is now read-only.
/
utils.go
168 lines (144 loc) · 3.75 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
159
160
161
162
163
164
165
166
167
168
package neoutils
import (
"crypto/sha256"
"encoding/binary"
"encoding/hex"
"fmt"
"math/big"
nep9 "github.com/o3labs/NEP9-go/nep9"
"github.com/o3labs/neo-utils/neoutils/btckey"
"golang.org/x/crypto/ripemd160"
)
func ReverseBytes(b []byte) []byte {
for i := 0; i < len(b)/2; i++ {
j := len(b) - i - 1
b[i], b[j] = b[j], b[i]
}
return b
}
// Simple hex string to bytes
func HexTobytes(hexstring string) (b []byte) {
b, _ = hex.DecodeString(hexstring)
return b
}
// Simple bytes to Hex
func BytesToHex(b []byte) string {
return hex.EncodeToString(b)
}
// Convert script hash to NEO address
// This method takes Big Endian Script hash
func ScriptHashToNEOAddress(scriptHash string) string {
b := HexTobytes(scriptHash)
//script hash from rpc or anything is always in big endian
//to convert to a proper neo address
//we need to reverse it first
address := btckey.B58checkencodeNEO(0x17, ReverseBytes(b))
return address
}
// // Convert NEO address to script hash
// func NEOAddressToScriptHash(neoAddress string) string {
// v, b, _ := btckey.B58checkdecode(neoAddress)
// if v != 0x17 {
// return ""
// }
// //reverse from little endian to big endian
// return fmt.Sprintf("%x", ReverseBytes(b))
// }
// Convert NEO address to script hash
func NEOAddressToScriptHashWithEndian(neoAddress string, endian binary.ByteOrder) string {
v, b, _ := btckey.B58checkdecode(neoAddress)
if v != 0x17 {
return ""
}
if endian == binary.LittleEndian {
return fmt.Sprintf("%x", b)
} else {
//reverse from little endian to big endian
return fmt.Sprintf("%x", ReverseBytes(b))
}
}
// Validate NEO address
func ValidateNEOAddress(address string) bool {
//NEO address version is 23
//https://github.com/neo-project/neo/blob/427a3cd08f61a33e98856e4b4312b8147708105a/neo/protocol.json#L4
ver, _, err := btckey.B58checkdecode(address)
if err != nil {
return false
}
if ver != 23 {
return false
}
return true
}
// Convert byte array to big int
func ConvertByteArrayToBigInt(hexString string) *big.Int {
b, err := hex.DecodeString(hexString)
if err != nil {
return nil
}
reversed := ReverseBytes(b)
v := new(big.Int).SetBytes(reversed)
return v
}
type SimplifiedNEP9 struct {
To string `json:"to"`
Asset string `json:"assetID"`
Amount float64 `json:"amount"`
}
func ParseNEP9URI(uri string) (*SimplifiedNEP9, error) {
parsed, err := nep9.NewURI(uri)
if err != nil {
return nil, err
}
return &SimplifiedNEP9{
To: parsed.Address,
Asset: parsed.Asset,
Amount: parsed.Amount,
}, nil
}
func Hash160(data []byte) []byte {
_, b, err := btckey.B58checkdecode(string(data))
if err != nil {
return nil
}
shortened := b[1 : len(b)-1]
hex := bytesToHex(shortened)
return ReverseBytes([]byte(hex))
}
func Hash256(b []byte) []byte {
hash := sha256.Sum256(b)
hash = sha256.Sum256(hash[:])
return hash[:]
}
func PublicKeyToNEOAddress(publicKeyBytes []byte) string {
publicKeyBytes = append([]byte{0x21}, publicKeyBytes...)
publicKeyBytes = append(publicKeyBytes, 0xAC)
/* SHA256 Hash */
sha256_h := sha256.New()
sha256_h.Reset()
sha256_h.Write(publicKeyBytes)
pub_hash_1 := sha256_h.Sum(nil)
/* RIPEMD-160 Hash */
ripemd160_h := ripemd160.New()
ripemd160_h.Reset()
ripemd160_h.Write(pub_hash_1)
pub_hash_2 := ripemd160_h.Sum(nil)
program_hash := pub_hash_2
address := btckey.B58checkencodeNEO(0x17, program_hash)
return address
}
func VMCodeToNEOAddress(vmCode []byte) string {
/* SHA256 Hash */
sha256_h := sha256.New()
sha256_h.Reset()
sha256_h.Write(vmCode)
pub_hash_1 := sha256_h.Sum(nil)
/* RIPEMD-160 Hash */
ripemd160_h := ripemd160.New()
ripemd160_h.Reset()
ripemd160_h.Write(pub_hash_1)
pub_hash_2 := ripemd160_h.Sum(nil)
program_hash := pub_hash_2
address := btckey.B58checkencodeNEO(0x17, program_hash)
return address
}