forked from echovl/cardano-go
-
Notifications
You must be signed in to change notification settings - Fork 1
/
encoding.go
53 lines (43 loc) · 1.08 KB
/
encoding.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
package cardano
import (
"encoding/hex"
"fmt"
"github.com/safanaj/cardano-go/internal/cbor"
)
var cborEnc, _ = cbor.CanonicalEncOptions().EncMode()
var cborDec, _ = cbor.DecOptions{MapKeyByteString: cbor.MapKeyByteStringWrap}.DecMode()
func GetCBOR_EncMode() cbor.EncMode { return cborEnc }
func GetCBOR_DecMode() cbor.DecMode { return cborDec }
func getTypeFromCBORArray(data []byte) (uint64, error) {
raw := []interface{}{}
if err := cborDec.Unmarshal(data, &raw); err != nil {
return 0, err
}
if len(raw) == 0 {
return 0, fmt.Errorf("empty CBOR array")
}
t, ok := raw[0].(uint64)
if !ok {
return 0, fmt.Errorf("invalid Type")
}
return t, nil
}
func GetBytesFromCBORHex(cborHex string) ([]byte, error) {
cborData, err := hex.DecodeString(cborHex)
if err != nil {
return nil, err
}
var data []byte
err = cborDec.Unmarshal(cborData, &data)
if err != nil {
return nil, err
}
return data, nil
}
func GetCBORHexFromBytes(data []byte) (string, error) {
cborData, err := cborEnc.Marshal(data)
if err != nil {
return "", err
}
return hex.EncodeToString(cborData), nil
}