/
helpers.go
110 lines (87 loc) · 2.33 KB
/
helpers.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
//
// Copyright 2019 Wireline, Inc.
//
package helpers
import (
"bytes"
"crypto/sha256"
"encoding/base64"
"encoding/hex"
"encoding/json"
cbor "github.com/ipfs/go-ipld-cbor"
mh "github.com/multiformats/go-multihash"
"github.com/tendermint/tendermint/crypto"
"golang.org/x/crypto/ripemd160"
)
// MarshalMapToJSONBytes converts map[string]interface{} to bytes.
func MarshalMapToJSONBytes(val map[string]interface{}) (bytes []byte) {
bytes, err := json.Marshal(val)
if err != nil {
panic("Marshal error.")
}
return
}
// UnMarshalMapFromJSONBytes converts bytes to map[string]interface{}.
func UnMarshalMapFromJSONBytes(bytes []byte) map[string]interface{} {
var val map[string]interface{}
err := json.Unmarshal(bytes, &val)
if err != nil {
panic("Marshal error.")
}
return val
}
// GetCid gets the content ID.
func GetCid(content []byte) (string, error) {
node, err := cbor.FromJSON(bytes.NewReader(content), mh.SHA2_256, -1)
if err != nil {
return "", err
}
return node.Cid().String(), nil
}
// GetAddressFromPubKey gets an address from the public key.
func GetAddressFromPubKey(pubKey crypto.PubKey) string {
hasherSHA256 := sha256.New()
hasherSHA256.Write(pubKey.Bytes())
sha := hasherSHA256.Sum(nil)
hasherRIPEMD160 := ripemd160.New()
hasherRIPEMD160.Write(sha)
ripemd := hasherRIPEMD160.Sum(nil)
return BytesToHex(ripemd)
}
// BytesToBase64 encodes a byte array as a base64 string.
func BytesToBase64(bytes []byte) string {
return base64.StdEncoding.EncodeToString(bytes)
}
// BytesFromBase64 decodes a byte array from a base64 string.
func BytesFromBase64(str string) []byte {
bytes, err := base64.StdEncoding.DecodeString(str)
if err != nil {
panic("Error decoding string to bytes.")
}
return bytes
}
// BytesToHex encodes a byte array as a hex string.
func BytesToHex(bytes []byte) string {
return hex.EncodeToString(bytes)
}
// BytesFromHex decodes a byte array from a hex string.
func BytesFromHex(str string) []byte {
bytes, err := hex.DecodeString(str)
if err != nil {
panic("Error decoding hex to bytes.")
}
return bytes
}
// Intersection computes the intersection of two string slices.
func Intersection(a, b []string) (c []string) {
m := make(map[string]bool)
for _, item := range a {
m[item] = true
}
for _, item := range b {
if _, ok := m[item]; ok {
c = append(c, item)
}
}
return
}