Allow 0x difficulty decoding, change errors for key/hash/difficulty size

This commit is contained in:
DataHoarder 2023-01-15 09:07:51 +01:00
parent 2ef7a5e1da
commit 4986272243
Signed by: DataHoarder
SSH key fingerprint: SHA256:OLTRf6Fl87G52SiR7sWLGNzlJt4WOX+tfI2yxo0z7xk
3 changed files with 24 additions and 12 deletions

View file

@ -75,7 +75,7 @@ func (p *PrivateKeyScalar) UnmarshalJSON(b []byte) error {
return err
} else {
if len(buf) != PrivateKeySize {
return errors.New("wrong hash size")
return errors.New("wrong key size")
}
if _, err = p.Scalar().SetCanonicalBytes(buf); err != nil {
@ -127,7 +127,7 @@ func (k *PrivateKeyBytes) UnmarshalJSON(b []byte) error {
return err
} else {
if len(buf) != PrivateKeySize {
return errors.New("wrong hash size")
return errors.New("wrong key size")
}
copy((*k)[:], buf)
@ -177,7 +177,7 @@ func (k *PrivateKeySlice) UnmarshalJSON(b []byte) error {
return err
} else {
if len(buf) != PrivateKeySize {
return errors.New("wrong hash size")
return errors.New("wrong key size")
}
*k = buf

View file

@ -71,7 +71,7 @@ func (k *PublicKeyPoint) UnmarshalJSON(b []byte) error {
return err
} else {
if len(buf) != PublicKeySize {
return errors.New("wrong hash size")
return errors.New("wrong key size")
}
if _, err = k.Point().SetBytes(buf); err != nil {
@ -110,7 +110,7 @@ func (k *PublicKeyBytes) UnmarshalJSON(b []byte) error {
return err
} else {
if len(buf) != PublicKeySize {
return errors.New("wrong hash size")
return errors.New("wrong key size")
}
copy((*k)[:], buf)
@ -147,7 +147,7 @@ func (k *PublicKeySlice) UnmarshalJSON(b []byte) error {
return err
} else {
if len(buf) != PublicKeySize {
return errors.New("wrong hash size")
return errors.New("wrong key size")
}
*k = buf

View file

@ -9,6 +9,7 @@ import (
"lukechampine.com/uint128"
"math/big"
"math/bits"
"strings"
)
const DifficultySize = 16
@ -189,14 +190,25 @@ func (d Difficulty) MarshalJSON() ([]byte, error) {
}
func DifficultyFromString(s string) (Difficulty, error) {
if buf, err := hex.DecodeString(s); err != nil {
return ZeroDifficulty, err
} else {
if len(buf) != DifficultySize {
return ZeroDifficulty, errors.New("wrong hash size")
if strings.HasPrefix(s, "0x") {
if buf, err := hex.DecodeString(s[2:]); err != nil {
return ZeroDifficulty, err
} else {
//TODO: check this
var d [DifficultySize]byte
copy(d[DifficultySize-len(buf):], buf)
return DifficultyFromBytes(d[:]), nil
}
} else {
if buf, err := hex.DecodeString(s); err != nil {
return ZeroDifficulty, err
} else {
if len(buf) != DifficultySize {
return ZeroDifficulty, errors.New("wrong difficulty size")
}
return DifficultyFromBytes(buf), nil
return DifficultyFromBytes(buf), nil
}
}
}