Fix public key decoding

This commit is contained in:
mariusor 2021-08-16 16:28:43 +02:00
parent 06ac3605b8
commit 94c953e391
2 changed files with 27 additions and 10 deletions

View file

@ -195,16 +195,13 @@ type PublicKey struct {
}
func (p *PublicKey) UnmarshalJSON(data []byte) error {
if id := fastjson.GetString(data, "id"); len(id) > 0 {
p.ID = ID(id)
par := fastjson.Parser{}
val, err := par.ParseBytes(data)
if err != nil {
return err
}
if o := fastjson.GetString(data, "owner"); len(o) > 0 {
p.Owner = IRI(o)
}
if pub := fastjson.GetString(data, "publicKeyPem"); len(pub) > 0 {
p.PublicKeyPem = pub
}
return nil
return loadPublicKey(val, p)
}
func (p PublicKey) MarshalJSON() ([]byte, error) {

View file

@ -121,7 +121,14 @@ func JSONGetDuration(val *fastjson.Value, prop string) time.Duration {
func JSONGetPublicKey(val *fastjson.Value, prop string) PublicKey {
key := PublicKey{}
key.UnmarshalJSON(JSONGetBytes(val, prop))
if val == nil {
return key
}
val = val.Get(prop)
if val == nil {
return key
}
loadPublicKey(val, &key)
return key
}
@ -619,3 +626,16 @@ func loadLink(val *fastjson.Value, l *Link) error {
}
return nil
}
func loadPublicKey(val *fastjson.Value, p *PublicKey) error {
if id := val.GetStringBytes("id"); len(id) > 0 {
p.ID = ID(id)
}
if o := val.GetStringBytes("owner"); len(o) > 0 {
p.Owner = IRI(o)
}
if pub := val.GetStringBytes("publicKeyPem"); len(pub) > 0 {
p.PublicKeyPem = string(pub)
}
return nil
}