laconicd-deprecated/x/registry/helpers/helpers.go
Nabarun 776799ea02
Some checks failed
Pull Request Labeler / triage (push) Successful in 58s
Run Gosec / Gosec (push) Failing after 2m29s
Tests / cleanup-runs (push) Has been skipped
Lint / Run flake8 on python integration tests (push) Failing after 4m48s
Tests / test-unit-cover (push) Failing after 8m52s
Tests / sdk_tests (push) Failing after 22s
Tests / test-rpc (push) Failing after 1m18s
Lint / Run golangci-lint (push) Successful in 15m15s
Tests / test-importer (push) Successful in 11m6s
CodeQL / Analyze (go) (push) Failing after 3m14s
Semgrep / Scan (push) Failing after 47s
Remove hard-coded record types (#132)
- Resolves #107 and is initial work on #108
  - Refactors the `Record.Attributes` from Any into a byte string and removes the hard-coded Protobuf record types.
  - Fixes EIP-712 bytes decoding.
- Resolves #109
  - Rewords the graphql schema to be able to represent generic IPLD objects encoded as DAG-JSON.

Co-authored-by: Roy Crihfield <roy@manteia.ltd>
Co-authored-by: neeraj <neeraj.rtly@gmail.com>
Co-authored-by: Prathamesh Musale <prathamesh.musale0@gmail.com>
Reviewed-on: cerc-io/laconicd#132
Reviewed-by: Thomas E Lackey <telackey@noreply.git.vdb.to>
Co-authored-by: Nabarun <nabarun@deepstacksoft.com>
Co-committed-by: Nabarun <nabarun@deepstacksoft.com>
2024-01-15 04:20:38 +00:00

134 lines
2.6 KiB
Go

package helpers
import (
"bytes"
"encoding/base64"
"encoding/binary"
"encoding/gob"
"encoding/hex"
"encoding/json"
"sort"
wnsUtils "github.com/cerc-io/laconicd/utils"
set "github.com/deckarep/golang-set"
)
func StringToBytes(val string) []byte {
return []byte(val)
}
func BytesToString(val []byte) string {
return string(val)
}
func StrArrToBytesArr(val []string) ([]byte, error) {
buffer := &bytes.Buffer{}
err := gob.NewEncoder(buffer).Encode(val)
if err != nil {
return nil, err
}
return buffer.Bytes(), nil
}
func BytesArrToStringArr(val []byte) ([]string, error) {
buffer := bytes.NewReader(val)
var v []string
err := gob.NewDecoder(buffer).Decode(&v)
if err != nil {
return nil, err
}
return v, nil
}
func Int64ToBytes(num int64) []byte {
buf := new(bytes.Buffer)
_ = binary.Write(buf, binary.BigEndian, num)
return buf.Bytes()
}
func MustMarshalJSON[T any](val T) (bytes []byte) {
bytes, err := json.Marshal(val)
if err != nil {
panic("JSON marshal error:" + err.Error())
}
return
}
func MustUnmarshalJSON[T any](bytes []byte) T {
var val T
err := json.Unmarshal(bytes, &val)
if err != nil {
panic("JSON unmarshal error:" + err.Error())
}
return val
}
// GetCid gets the content ID.
func GetCid(content []byte) (string, error) {
return wnsUtils.CIDFromJSONBytes(content)
}
// 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
}
func SetToSlice(set set.Set) []string {
names := []string{}
for name := range set.Iter() {
if name, ok := name.(string); ok && name != "" {
names = append(names, name)
}
}
sort.SliceStable(names, func(i, j int) bool { return names[i] < names[j] })
return names
}
func SliceToSet(names []string) set.Set {
set := set.NewThreadUnsafeSet()
for _, name := range names {
if name != "" {
set.Add(name)
}
}
return set
}
func AppendUnique(list []string, element string) []string {
set := SliceToSet(list)
set.Add(element)
return SetToSlice(set)
}