-
Notifications
You must be signed in to change notification settings - Fork 52
/
Copy pathutils.go
88 lines (71 loc) · 2.18 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
package blockchain
import (
"encoding/base64"
"fmt"
"github.com/ethereum/go-ethereum/common"
log "github.com/sirupsen/logrus"
"regexp"
"strings"
)
// ParseSignature parses Ethereum signature.
func ParseSignature(jobSignatureBytes []byte) (uint8, [32]byte, [32]byte, error) {
r := [32]byte{}
s := [32]byte{}
if len(jobSignatureBytes) != 65 {
return 0, r, s, fmt.Errorf("job signature incorrect length")
}
v := uint8(jobSignatureBytes[64])%27 + 27
copy(r[:], jobSignatureBytes[0:32])
copy(s[:], jobSignatureBytes[32:64])
return v, r, s, nil
}
// AddressToHex converts Ethereum address to hex string representation.
func AddressToHex(address *common.Address) string {
return address.Hex()
}
// BytesToBase64 converts array of bytes to base64 string.
func BytesToBase64(bytes []byte) string {
return base64.StdEncoding.EncodeToString(bytes)
}
// HexToBytes converts hex string to bytes array.
func HexToBytes(str string) []byte {
return common.FromHex(str)
}
// HexToAddress converts hex string to Ethreum address.
func HexToAddress(str string) common.Address {
return common.Address(common.BytesToAddress(HexToBytes(str)))
}
func StringToBytes32(str string) [32]byte {
var byte32 [32]byte
copy(byte32[:], []byte(str))
return byte32
}
func RemoveSpecialCharactersfromHash(pString string) string {
reg, err := regexp.Compile("[^a-zA-Z0-9=]")
if err != nil {
log.Panic(err)
}
return reg.ReplaceAllString(pString, "")
}
func ConvertBase64Encoding(str string) ([32]byte, error) {
var byte32 [32]byte
data, err := base64.StdEncoding.DecodeString(str)
if err != nil {
log.WithError(err).WithField("String Passed:", str)
return byte32, err
}
copy(byte32[:], data[:])
return byte32, nil
}
func FormatHash(ipfsHash string) string {
log.WithField("metadataHash", ipfsHash).Debug("Before Formatting")
ipfsHash = strings.Replace(ipfsHash, IpfsPrefix, "", -1)
ipfsHash = RemoveSpecialCharactersfromHash(ipfsHash)
log.WithField("metadataUri", ipfsHash).Debug("After Formatting")
return ipfsHash
}
func ToChecksumAddress(hexAddress string) string {
address := common.HexToAddress(hexAddress)
mixedAddress := common.NewMixedcaseAddress(address)
return mixedAddress.Address().String()
}