-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #10 from 18aaddy/fix/implement-common-utils-#3
Implement common utils #3
- Loading branch information
Showing
1 changed file
with
36 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
) | ||
|
||
// 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) | ||
} |