Optimize JSON decoding of Hash / Address / PublicKeyBytes / PrivateKeyBytes
All checks were successful
continuous-integration/drone/push Build is passing

This commit is contained in:
DataHoarder 2023-05-24 15:59:07 +02:00
parent 5e9bc7e825
commit c32f87e9b4
Signed by: DataHoarder
SSH key fingerprint: SHA256:OLTRf6Fl87G52SiR7sWLGNzlJt4WOX+tfI2yxo0z7xk
4 changed files with 51 additions and 28 deletions

View file

@ -83,6 +83,35 @@ func FromBase58(address string) *Address {
return a
}
func FromBase58NoChecksumCheck(address string) *Address {
raw := moneroutil.DecodeMoneroBase58(address)
if len(raw) != 69 {
return nil
}
switch raw[0] {
case moneroutil.MainNetwork, moneroutil.TestNetwork, moneroutil.StageNetwork:
break
case moneroutil.IntegratedMainNetwork, moneroutil.IntegratedTestNetwork, moneroutil.IntegratedStageNetwork:
return nil
case moneroutil.SubAddressMainNetwork, moneroutil.SubAddressTestNetwork, moneroutil.SubAddressStageNetwork:
return nil
default:
return nil
}
a := &Address{
Network: raw[0],
checksum: raw[65:],
}
copy(a.SpendPub[:], raw[1:33])
copy(a.ViewPub[:], raw[33:65])
return a
}
func FromRawAddress(network uint8, spend, view crypto.PublicKey) *Address {
var nice [69]byte
nice[0] = network
@ -125,7 +154,7 @@ func (a *Address) UnmarshalJSON(b []byte) error {
return err
}
if addr := FromBase58(s); addr != nil {
if addr := FromBase58NoChecksumCheck(s); addr != nil {
a.Network = addr.Network
a.SpendPub = addr.SpendPub
a.ViewPub = addr.ViewPub

View file

@ -150,19 +150,17 @@ func (k *PrivateKeyBytes) Value() (driver.Value, error) {
}
func (k *PrivateKeyBytes) UnmarshalJSON(b []byte) error {
var s string
if err := json.Unmarshal(b, &s); err != nil {
return err
if len(b) == 0 || len(b) == 2 {
return nil
}
if buf, err := hex.DecodeString(s); err != nil {
if len(b) != PrivateKeySize*2+2 {
return errors.New("wrong key size")
}
if _, err := hex.Decode(k[:], b[1:len(b)-1]); err != nil {
return err
} else {
if len(buf) != PrivateKeySize {
return errors.New("wrong key size")
}
copy((*k)[:], buf)
return nil
}
}

View file

@ -133,19 +133,17 @@ func (k *PublicKeyBytes) Value() (driver.Value, error) {
}
func (k *PublicKeyBytes) UnmarshalJSON(b []byte) error {
var s string
if err := json.Unmarshal(b, &s); err != nil {
return err
if len(b) == 0 || len(b) == 2 {
return nil
}
if buf, err := hex.DecodeString(s); err != nil {
if len(b) != PublicKeySize*2+2 {
return errors.New("wrong key size")
}
if _, err := hex.Decode(k[:], b[1:len(b)-1]); err != nil {
return err
} else {
if len(buf) != PublicKeySize {
return errors.New("wrong key size")
}
copy((*k)[:], buf)
return nil
}
}

View file

@ -122,19 +122,17 @@ func (h *Hash) Value() (driver.Value, error) {
}
func (h *Hash) UnmarshalJSON(b []byte) error {
var s string
if err := json.Unmarshal(b, &s); err != nil {
return err
if len(b) == 0 || len(b) == 2 {
return nil
}
if buf, err := hex.DecodeString(s); err != nil {
if len(b) != HashSize*2+2 {
return errors.New("wrong hash size")
}
if _, err := hex.Decode(h[:], b[1:len(b)-1]); err != nil {
return err
} else {
if len(buf) != HashSize {
return errors.New("wrong hash size")
}
copy(h[:], buf)
return nil
}
}