Skip to content

Commit

Permalink
Merge pull request #10 from 18aaddy/fix/implement-common-utils-#3
Browse files Browse the repository at this point in the history
Implement common utils #3
  • Loading branch information
star-gazer111 authored Aug 25, 2024
2 parents c89be79 + c9ffa51 commit 5c8e261
Showing 1 changed file with 36 additions and 6 deletions.
42 changes: 36 additions & 6 deletions common/utils.go
Original file line number Diff line number Diff line change
@@ -1,19 +1,49 @@
package common

import (
"encoding/hex"
"encoding/json"
"fmt"
"strings"

"github.com/ethereum/go-ethereum/common"

Check failure on line 9 in common/utils.go

View workflow job for this annotation

GitHub Actions / build (1.22.3)

no required module provides package github.com/ethereum/go-ethereum/common; to add it:
)

// if we need to export the functions , just make their first letter capitalised
func hex_str_to_bytes() {
func Hex_str_to_bytes(s string) ([]byte, error) {
s = strings.TrimPrefix(s, "0x")

bytesArray, err := hex.DecodeString(s)

if err != nil {
return nil, err
}

return bytesArray, nil
}

func address_to_hex_string() {
func Address_to_hex_string(addr common.Address) string {
bytesArray := addr.Bytes()
return fmt.Sprintf("0x%x", hex.EncodeToString(bytesArray))
}

func u64_to_hex_string() {
func U64_to_hex_string(val uint64) string {
return fmt.Sprintf("0x%x", val)
}

func bytes_deserialize() {
func Bytes_deserialize(data []byte) ([]byte, error) {
var hexString string
if err := json.Unmarshal(data, &hexString); err != nil {
return nil, err
}

return Hex_str_to_bytes(hexString)
}

func bytes_serialize() {

func Bytes_serialize(bytes []byte) ([]byte, error) {
if bytes == nil {
return json.Marshal(nil)
}
hexString := hex.EncodeToString(bytes)
return json.Marshal(hexString)
}

0 comments on commit 5c8e261

Please sign in to comment.