2020-11-17 22:37:43 +00:00
|
|
|
package interchangeformat
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/hex"
|
|
|
|
"fmt"
|
|
|
|
"strconv"
|
|
|
|
"strings"
|
|
|
|
)
|
|
|
|
|
|
|
|
func uint64FromString(str string) (uint64, error) {
|
|
|
|
return strconv.ParseUint(str, 10, 64)
|
|
|
|
}
|
|
|
|
|
|
|
|
func pubKeyFromHex(str string) ([48]byte, error) {
|
|
|
|
pubKeyBytes, err := hex.DecodeString(strings.TrimPrefix(str, "0x"))
|
|
|
|
if err != nil {
|
|
|
|
return [48]byte{}, err
|
|
|
|
}
|
|
|
|
if len(pubKeyBytes) != 48 {
|
2020-11-20 22:33:51 +00:00
|
|
|
return [48]byte{}, fmt.Errorf("public key is not correct, 48-byte length: %s", str)
|
2020-11-17 22:37:43 +00:00
|
|
|
}
|
|
|
|
var pk [48]byte
|
|
|
|
copy(pk[:], pubKeyBytes[:48])
|
|
|
|
return pk, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func rootFromHex(str string) ([32]byte, error) {
|
|
|
|
rootHexBytes, err := hex.DecodeString(strings.TrimPrefix(str, "0x"))
|
|
|
|
if err != nil {
|
|
|
|
return [32]byte{}, err
|
|
|
|
}
|
|
|
|
if len(rootHexBytes) != 32 {
|
2020-11-20 22:33:51 +00:00
|
|
|
return [32]byte{}, fmt.Errorf("wrong root length, 32-byte length: %s", str)
|
2020-11-17 22:37:43 +00:00
|
|
|
}
|
|
|
|
var root [32]byte
|
|
|
|
copy(root[:], rootHexBytes[:32])
|
|
|
|
return root, nil
|
|
|
|
}
|