Skip to content

Commit

Permalink
Add MarshalJSON for tlb.ValidatorDescr
Browse files Browse the repository at this point in the history
  • Loading branch information
aleksej-paschenko committed Oct 26, 2023
1 parent 3589fcf commit f430273
Showing 1 changed file with 24 additions and 0 deletions.
24 changes: 24 additions & 0 deletions tlb/validators.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
package tlb

import (
"encoding/json"
"fmt"
)

// validator_info$_
//
// validator_list_hash_short:uint32
Expand Down Expand Up @@ -72,6 +77,25 @@ type ValidatorDescr struct {
} `tlbSumType:"validatoraddr#73"`
}

func (vd ValidatorDescr) MarshalJSON() ([]byte, error) {
switch vd.SumType {
case "Validator":
bytes, err := json.Marshal(vd.Validator)
if err != nil {
return nil, err
}
return []byte(fmt.Sprintf(`{"SumType": "Validator","Validator":%v}`, string(bytes))), nil
case "ValidatorAddr":
bytes, err := json.Marshal(vd.ValidatorAddr)
if err != nil {
return nil, err
}
return []byte(fmt.Sprintf(`{"SumType": "ValidatorAddr","ValidatorAddr":%v}`, string(bytes))), nil
default:
return nil, fmt.Errorf("unknown sum type %v", vd.SumType)
}
}

func (vd ValidatorDescr) PubKey() Bits256 {
if vd.SumType == "Validator" {
return vd.Validator.PublicKey.PubKey
Expand Down

0 comments on commit f430273

Please sign in to comment.