Initial cloud-services repo - gateway service + pkg modules
This commit is contained in:
56
pkg/common/binary_hex.go
Normal file
56
pkg/common/binary_hex.go
Normal file
@@ -0,0 +1,56 @@
|
||||
package common
|
||||
|
||||
import (
|
||||
"encoding/hex"
|
||||
"fmt"
|
||||
"strings"
|
||||
)
|
||||
|
||||
func NewBinaryHex(data []byte) BinaryHex {
|
||||
var result BinaryHex = data
|
||||
return result
|
||||
}
|
||||
|
||||
type BinaryHex []byte
|
||||
|
||||
func (bh BinaryHex) Bytes() []byte {
|
||||
return []byte(bh)
|
||||
}
|
||||
|
||||
func (bh *BinaryHex) SetBytes(value []byte) {
|
||||
v := BinaryHex(value)
|
||||
*bh = v
|
||||
}
|
||||
|
||||
func (bh BinaryHex) String() string {
|
||||
return hex.EncodeToString(bh)
|
||||
}
|
||||
|
||||
// TODO Hack to render []byte as int array for the JSON RPC. Otherwise Go renders in base64
|
||||
func (bh BinaryHex) UintArray() []uint {
|
||||
bytes := bh.Bytes()
|
||||
result := make([]uint, len(bytes))
|
||||
for i, d := range bytes {
|
||||
result[i] = uint(d)
|
||||
}
|
||||
|
||||
return result
|
||||
}
|
||||
|
||||
func (bh *BinaryHex) SetHex(data string) error {
|
||||
x, err := hex.DecodeString(strings.Trim(string(data), `"`))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
*bh = x
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (bh BinaryHex) MarshalJSON() ([]byte, error) {
|
||||
return []byte(fmt.Sprintf(`"%s"`, bh.String())), nil
|
||||
}
|
||||
|
||||
func (bh *BinaryHex) UnmarshalJSON(data []byte) error {
|
||||
return bh.SetHex(string(data))
|
||||
}
|
||||
Reference in New Issue
Block a user